[Haskell-cafe] Re: Embedding newlines into a string?

Tillmann Rendel rendel at daimi.au.dk
Tue Apr 15 03:14:53 EDT 2008


Benjamin L. Russel wrote:
> hanoi_shower ((a, b) : moves)
>     | null moves = ...
>     | otherwise == ...

Luke Palmer wrote:
> More idiomatic pedantry:  the way you will see most Haskellers write
> this style of function is by pattern matching rather than guards:
> 
> hanoi_shower [] = ...
> hanoi_shower ((a,b):moves) = ...

These two versions are semantically different! Benjamin's versions works 
for lists of length 1 or more, Luke's version works for lists of length 
0 or more.

Luke's version looks like a typical Haskell solution, which would be 
expressed in lispy syntax like this:

(define hanoi_shower (lambda (xs)
   (cond ((null xs) (...))
         (true, (let ((a,     (first (first xs)))
                      (b,     (rest (first xs)))
                      (moves, (rest xs)))
                        (...)))))

The pattern matching in Haskell takes care of both the cond and the let, 
there's no need for guards or to actually call null or any selector 
functions. A nice exercise may be to implement the map function using 
primitive recursion.

   Tillmann


More information about the Haskell-Cafe mailing list