Thursday, April 02, 2009
Book Review: Real World Haskell
"Real World Haskell? Isn't that an oxymoron?" I heard the question asked in one way or another many times as I lugged the book between meetings (looking for spare minutes to read it). As the authors explained in my Real World Haskell interview, Functional Programming languages generally and Haskell specifically, might have once be confined to the ivory tower but no longer. And this book is one great way to help bring the benefits of haskell to your coding projects.
I'm still not sold on Static vs. Dynamic Typing, and Ruby remains my language of choice, but I've got to say that Haskell is not nearly as intimidating as it once was. Maybe with enough intentional use it will be a tool I reach for more often without having to think about it.
Real World Haskell is a big, solid book with a lot to commend it. It's well organized, easy to read, and loaded with good examples. Best of all, it's written by long-term members of the Haskell community, so you're getting idiomatic code and well reasoned explanations by guys who have been there. The only down side is a somewhat weak index.
If you're a rubyist looking to understand more about Haskell or Functional Programming in general, this is the book for you. In fact, if you're a rubyist, you should be looking at this kind of book in general. I can't wait to see RWH reading groups start up within Ruby Brigades ... it will certainly make us better programmers.
Click here to Tweet this article
This post is sponsored by Cowork Utah, a member-based alternative for “cubicle adverse”solopreneurs, writers, web developers, designers and other independents who prefer working in a stimulating environment away from home. We’re free agents who collaborate while maintaining our autonomy. Specifically, our primary focus revolves around web 2.0 social media tools and strategies.
Thursday, March 12, 2009
Beginning Scala -- Author Interview with David Pollak
There are several good looking Scala books on their way to market. I'd like to help you get an early look at the books and their authors, so I'm working on a series of Scala author interviews for the FP side of On Ruby. Here's the first of several interviews.
I've spent some time talking with David Pollak (@dpp) about his upcoming book from Apress, Beginning Scala (also available, electronicly, as an Alpha book from Apress). Read on to see what he has to say.
Functional Programming languages seem to be an increasingly popular topic. Why? What do FP languages teach or do for us?
David I came to Scala via Ruby. I was doing some Ruby and some Java coding (I've been doing Java since 1996). My Java code started taking on a "Ruby Flavor" in that I was passing around a lot of code blocks. In Ruby, passing code block, lambdas, etc. is super-simple. In Java, I was creating lots of anonymous inner classes and passing them around... it was a syntactic disaster.
In Scala, passing a function, a block of code, as a parameter is as easy and syntactically pleasing as it is in Ruby (or nearly so... Charlie Nutter has been barfing all over Scala's syntax lately.) This is the first part of what I understand FP to be. So, in a way, Ruby is a functional language.
In addition to the simple ability to pass code blocks as parameters, other touch-stones of FP style coding are:
- Immutable data structures
- Pattern Matching
- Optionally, a math-driven type system
- Optionally, lazy evaluation
Scala's default collections classes are immutable. Once an instance has been created, the value of that instance, whether is be a List, a Map (dictionary), etc. never changes. This is familiar to Java programmers. Java's String class is immutable. Once you create a String, you can never change it. Python requires immutable data structures for keys to dictionaries. Immutability introduces a new way of approaching a program. One thinks about methods as things that take input and return output and will always return the same output given the same input. In Ruby, String is mutable and there have been many a bug in my programs because some other method changed the value of a String out from under me.
Pattern matching provides a powerful way to use declarative syntax to express business logic. I'm include an example in the idomatic section of intercepting an incoming HTTP request using pattern matching.
Scala has a very powerful type system including a type inferencer. What that means is that you can define very powerful rules (e.g, when building a query, you can only pass a String into a column that is a VARCHAR) in the library, but the end-user gets the syntactic simplicity of Ruby.
Scala supports lazy evaluation at the language level and in the libraries. This means that only elements that are actually accessed are computed.
Functional languages teach us how to think in terms of transformation rather than getting/setting. Functional programming focuses the developer's eyes and mind on the business logic rather than the boilerplate of flow of control. Functional programming gives us different kinds of abstractions to compose than objects. Composing functions and performing operations on collections just as easily as performing operations on single items changes the focus of development to logic rather than mechanisms.
What makes Scala the right FP language for people to pick up?
David Scala along with OCaml and F# are the "practical" functional languages. You can get things done out of the gate with any of these languages. You don't have to learn a lot of theory to get things done with Scala, OCaml or F#. With these languages, you can dip your toe in the functional waters without having to take an immediate plunge. Neither of these things is true of Haskell or Erlang.
Scala and F# have the Java and CLR libraries immediately available which is a killer reason to choose either of these languages.
Scala has better OO abstractions than does F# or any other language. Scala seems to have integrated OO and a Type System in a way that no other language has.
Why is your book the right one for people to pick up and learn about Scala?
David I take a very simple approach to Scala. There's very little theory and a whole lot of simple examples and idioms. I've tried to make sure that the code is oriented toward practical users of Java as well as practical users of Ruby. The chapters are short and to the point. More of the code examples can be typed into the REPL (like irb) so the reader can get immediate feedback on the example and the language as a whole.
Why is the JVM a good platform for Scala and FP?
David:
- It just works
- It's very, very fast
- Excellent integration with existing Java libraries
- The ability to intermix Java and Scala (and Groovy and JRuby) in a single project
- The JVM is everywhere and it's well tuned everywhere
What kinds of problems are a good fit for Scala?
David Heh... everything from simple scripting on up to extremely complex systems. With the exception of Erlang's distributed capabilities and C's native device capabilities, I think that every other problem is well solved with Scala. But, I'm biased. :-)
Design patterns and algorithms look differently when implemented in different languages. Can you give us some examples of elegant patterns or algorithms in Scala?
David A BDD Test:
object BoxSpec extends Specification {
"A Box" can {
"be created from a Option. It is Empty if the option is None" in {
Box(None) mustBe Empty
}
"be created from a Option. It is Full(x) if the option is Some(x)" in {
Box(Some(1)) must_== Full(1)
}
A Multi-user web-based chat application:
case class Messages(m: List[String])
object ChatServer extends Actor with ListenerManager {
private var msgs: List[String] = Nil
protected def createUpdate = Messages(msgs)
override val highPriority: PartialFunction[Any, Unit] = {
case s: String =>
msgs ::= s
updateListeners()
}
this.start
}
class Chat extends CometActor with CometListenee {
private var msgs: List[String] = Nil
def registerWith = ChatServer
override def highPriority = {
case Messages(m) =>
msgs = m
reRender(false)
}
def render =
<div>
<ul>
{
msgs.reverse.map(m => <li>{m}</l>>)
}
</ul>
{
ajaxText("", s => {ChatServer ! s; Noop})
}
</div>
}
A REST API handler:
case Req(ApiPath :: "statuses" ::
"public_timeline" :: Nil,
this.method, GetRequest) => publicTimeline
def publicTimeline(): Box[TwitterResponse] = {
val statusList =
Message.findAll(OrderBy(Message.id,
Descending),
MaxRows(20)).
map(msgData _)
Full(Right(Map("statuses" ->
("status", statusList) )))
}
One more very nice piece of code... find all the classes used by <p> tags in an HTML document:
for {p <- x \\ "p"; ca <- p \ "@class" c <- ca.text.split(" ")} yield c
What tricks or tools do you suggest someone beginning to read Scala use?
David Don't just read, but use. Open the Scala REPL and start typing. Start playing. Start exploring. Try to understand the actual thing that happens when certain constructs are used. In this way, it's 100% the same as a newbie exploring Ruby. Type a line... see what happens... type another... see what happens. Touch, feel, play.
What are some good Scala code bases to read?
David The Lift utils packages is a nice place to start.
What blogs are good for learning more about Scala?
David
Monday, March 09, 2009
My Best Functional Programming Posts
What's this? A page about Functional Programming languages at On Ruby? Well, I've written about a number of non Ruby topics here, and there's enough FP stuff to warrant it's own page.
At one point, I also ran the On Erlang blog. I've decided to merge all of my computer related posts into On Ruby to keep myself sane.
I've looked mostly at Scala, Haskell and Erlang, with news, book reviews, and interviews about both. There's some OCaml stuff coming as well.
If you're a Rubyist looking to expand your horizons, there's probably something here for you. If you don't care about Ruby at all, don't let the blog's name scare you away.
I've probably done the most work in the Erlang community. I'm a pure beginner when it comes to writing Erlang code, but I like a lot of what I've seen and read. The community itself is especially nice. Here are some of my best (or most popular) posts about Erlang:
- Getting to know erlang-mode
- Practical Erlang Programming Mini Interview is a bit old, but it's a good conversation with two of the authors of O'Reilly's upcoming book.
- Concurrent Programming with Erlang/OTP - an early look — this one covers the book Manning has in the works.
- Reia- A New Dynamic Language on the Erlang VM is an interview with Tony Arcieri about his new language.
I've spent a little time learning more Haskell. The language intrigues me and the community is pretty strong as well.:
- Book Review: Real World Haskell — just what it says.
- Author Interview: Real World Haskell is an interview with the three authors of this great book.
- Real World Haskell: Pre-Reading Survey — before I read the book, I did a quick survey to think about what I wanted to learn from it.
Scala is a nice looking language that lives on the JVM. I've done some interviews with Scala book authors. I think I'll be getting some additional posts here soon.: