haskell-cafe@haskell.org
[Top] [All Lists]

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

Subject: Re: [Haskell-cafe] Re: Embedding newlines into a string?
From: Tillmann Rendel
Date: Tue, 15 Apr 2008 09:14:53 +0200
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
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@xxxxxxxxxxx
http://www.haskell.org/mailman/listinfo/haskell-cafe

<Prev in Thread] Current Thread [Next in Thread>