Day 7 - Haskell
Join the DZone community and get the full member experience.
Join For FreeI've reached the 7th and final language of Bruce Tate's Seven Languages in Seven Weeks.
While some of the previous languages were functional with some
imperative support, Haskell is a purely functional, and statically
typed, language. I'm looking forward to seeing how I will perform tasks
with no (or little -- more on that below) state.
Bruce starts
with the usual investigation of types, including looking at Haskell's
type inference, then moves on to functions. Functions are composed of a
type declaration and the function declaration. Because Haskell is
statically typed, a function must have a type, but the declaration isn't
strictly necessary because of Haskell's type inferencing. He covers
some examples using Erlang-familiar pattern matching and guards. As
with many of the other languages in this book, Haskell uses
tail-recursion optimization. Next we look at programming with tuples,
and using tuples to combine functions. We follow with a discussion of
Haskell lists, including generating them with recursion, ranges, and
list comprehension (a feature shared with Erlang). By the way, Bruce's
explanation of list comprehension here is one of the many reasons I like
this book. I didn't quite catch it the first time, but his elaboration
here is quite articulate.
Day 1 of Haskell finishes with an
interview with Philip Wadler, a member of the committee which designed
Haskell. One thing I took away from this interview was the variety of
organizations in which Haskell is being used, which is very encouraging.
I've been doing OO for 20 years now and I suppose I'm just looking
around for evidence that it can be safe and effective to give up mutable
state. I may actually decide to learn Haskell first, then pick up more
Scala, as a way of disciplining myself to stick with the paradigm.
Day
2 starts with higher-order functions, including anonymous functions and
partially-applied functions. Bruce then discusses some common
functions, like filter and the Haskell versions of fold-left and fold-right.
Bruce
spends a little more time discussing function composition. Every
function in Haskell has only one parameter, which, as you can guess,
leads to some interesting functional composition requirements to perform
any useful computation. You can define a function with multiple
parameters, but Haskell will split the function into multiple functions,
one with each argument, a procedure known as partial application, or
currying. Next he discusses lazy evaluation and how, as in Clojure, it
can be used to create infinite lists, where only the parts necessary to
perform a task are actually calculated. He finishes this section by
describing how problem solutions can be constructed by chaining/nesting
functions.
Haskell was created by committee. One side effect of
this process is that a number of talented individuals not only
contributed to the language, but are potentially available to share
their thoughts on the genesis of the language. Bruce next interviews
Simon Peyton-Jones, another member of the Haskell creation committee,
and the lead designer of the GHC compiler. Peyton-Jones makes one
observation that really sticks with me -- Haskell is a useful,
successful language, designed by committee! How often does that happen?
Day
3 begins with a deeper dive into Haskell's type system, which Bruce
calls one of its strongest features. As I mentioned earlier, the typing
is static, but Haskell is smart enough to infer types most of the time,
sparing some boilerplate. Developers can define their own data types
with the data keyword. He then discusses defining polymorphic
data types and recursive data types (the latter of which could be used
to define a tree, for example).
Next is a discussion of Haskell classes, with a side note that these are not
object-oriented classes. There is no data involved in a Haskell class!
So a class, as Bruce puts it, "defines which operations can work on
which inputs." So a type is an instance of a class if it supports all
the functions signatures which make up the definition of the class.
Monads
are discussed next. Bruce describes some example problems which are
awkward to formulate in a functional language as a motivation for the
use of monads, which are basically a technique of function composition.
When I referred earlier to absence of state, but with a qualification, I
was looking ahead to monads. Monads allow you to simulate state in a
Haskell program. He goes on to outline the components of a monad, which
are a container-based type constructor, a return function
which wraps a function into a monad, and a bind function that unwraps
the function. He then gives a few non-trivial monad-construction
examples, including the use of the Haskell do notation to add
some syntactic sugar. Bruce ends his Day 3 of Haskell with a discussion
of the language's core strengths and weaknesses.
My Personal Wrap-Up
I
started reading this book with some understanding and hands-on
experience with Scala, and a slight feeling of lost opportunity as a
result (Was this a good idea? Will I really learn functional
programming with a "compromise" language?). I still feel that way a
little, but I still expect to move forward with Scala as my bridge to
the FP world, although with a better understanding of what compromises I
am making in the process. I'm considering learning Haskell, at least
minimally, to see what it's like for me to work with a language that
doesn't have the Java/JVM escape hatch in it, so that I don't
automatically reach for that pill just because it's available.
Hopefully that experience will encourage me to use Scala's functional
side first, before reaching for the Java escape hatch.
Concerning
this book: Bruce Tate is a very articulate, clear and engaging writer.
I see a couple of books a year that speak very directly to me. Maybe I
think these books are superior simply because they happen to have been
written in a style that is especially suited to the way I think and
learn. With that acknowledgment of likely bias, I have to say Seven Languages in Seven Weeks
is definitely one of those "once or twice a year" books for me. I
really recommend you pick up a copy if you have a desire for a
shallow-dive into a variety of languages.
From http://wayne-adams.blogspot.com/2011/04/day-7-haskell.html
Opinions expressed by DZone contributors are their own.
Comments