[Haskell-cafe] Trouble with function with two clauses
blancolioni at gmail.com
Wed Jan 9 13:42:20 EST 2008
In head'', what is being compared to Nil?
The guards of a function are a series of Boolean expressions; but in your
example they are of type ConsCell a. The difference is that in a pattern,
the structure of the argument is matched; in a guard, an arbitrary
expression is evaluated.
I have always found the Haskell report instructive in these cases;
particularly the transformations into the core language -- in this case see
section 220.127.116.11 (http://haskell.org/onlinereport/decls.html#sect18.104.22.168);
this should make it clear why head'' is not valid Haskell.
Guards are really a series of Boolean equations, and the first that
evaluates to true
On Jan 9, 2008 7:15 PM, Fernando Rodriguez < frr149 at easyjob.net> wrote:
> I have the following type and function:
> data ConsCell a = Nil | Cons a (ConsCell a) deriving Show
> head' Nil = Nothing
> head' (Cons a _) = Just a
> Works fine, however, what's wrong with the following function?
> | Nil = Nothing
> | Cons a _ = Just a
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe