[Haskell-cafe] RE: Haskell as a first language?

Christopher Done chrisdone at googlemail.com
Thu Jul 16 14:40:01 EDT 2009

I believe that from Scheme to Haskell is a natural transition, as I
made the same transition myself. If you grasp the fundamental concepts
of Scheme, Haskell seems like a step up. I will describe Haskell in
terms of Scheme:

# Haskell programs are more correct from the ground up

Scheme will let you write (+ 2 "2") and only tell you that it's wrong
when it tries to run it ("You can't add a number and a string!"),
whereas Haskell won't even let you run it until the types are correct.
What some find difficult is ``1'' has type Num a => a, which means
"for all types a that are Numbers, a" (or just "all Number types"),
e.g. Integer, Double, Rational, etc. But this concept is found Scheme
-- in Scheme you have all sorts of number types; ``+'' means "add two
numbers". In Haskell, we specify that notion explicitly.

# Haskell is half wordy as Scheme

Indeed, that is what drew me to Haskell away from Scheme.

## Problem 1

Consider the problem ``Double all the numbers in a list." In Scheme we
might solve this problem like so:

    (map (lambda (n) (* n 2)) '(1 2 3 4)

and the result would be (2 4 6).

In Haskell, we can write the same:

    map (\n -> n * 2) [1,2,3,4]

and the result would be [2,4,6].

But we take it one step further:

map (*2) [1,2,3,4]

This means the same as the previous Haskell example. But the formal
parameter ``x'' has been tripped away because it is redundant.

## Problem 2

Another problem, ``Given a list of lists, reverse all the lists and
double each number in them.'' So in Scheme, we might solve this like

    (map (lambda (list)
                   (reverse (map (lambda (n) (* n 2)) list)))
             '(1 2 3 4 5))

In Haskell, the same solution is rendered thusly:

    map (reverse . map (*2)) [1,2,3,4]

The ``foo . bar'' is equivalent to (\x -> foo (bar x)), or, in Scheme
(lambda (x) (foo (bar x))). It's just that we have stripped the
redundant formal parameters.

## Problem 3

Suppose our problem is ``Multiply the first three items of a list''.
Our Scheme solution might be:

    (define (first-three-sum list) (* (car list) (cadr list) (caddr list)))

In Haskell, we may write (admittedly, a better way to render this
might be: firstThreeSum = sum . take 3):

    firstThreeSum (x:y:z:_) = x * y * z

Here I have demonstrated pattern matching, a powerful and oft-used
feature of Haskell for deconstructing a data structure, like a list or
a tuple, based on a pattern.

I'll abruptly stop here before I write a whole article. I'll summarize
by saying that Haskell is the next step, a natural progression where
things that you know all too well from Scheme become easier, and
safer. The stuff described here is just the tip of the ice burg, of
course. I wish that I had learned Haskell as my first programming
language! Haskell takes a lot of effort to learn, but gives back in
equal amounts. It is a rough ride to learn any programming language.
If you think otherwise, you are not learning, or are learning wrongly.

2009/7/14 Simon Peyton-Jones <simonpj at microsoft.com>
> Haskell is a great language!  Check out haskell.org.   I'm ccing the Haskell Cafe which is read by many people better qualified to answer your question than me.   (Since I've been working on Haskell for many years, I am not well qualified to say how it seems to a beginner.)
> S
> | -----Original Message-----
> | From: Charles Turner [mailto:charlie.h.turner at googlemail.com]
> | Sent: 11 July 2009 22:52
> | To: Simon Peyton-Jones
> | Subject: Haskell as a first language?
> |
> | I'll make this short! Do you think Haskell is a good language to start
> | with? I am brand new to programming and have been using Scheme, some of
> | my peers suggest I should use Haskell. It seems "professional" to me.
> | Has features that a beginner should not worry about. What would you
> | suggest. (I'm not worried about bias)
> |
> | Thank you very much for your time.
> |
> | Charles Turner.
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

More information about the Haskell-Cafe mailing list