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 4.4.3.2 (
http://haskell.org/onlinereport/decls.html#sect4.4.3.2); this should make it clear why head'' is not valid Haskell.
cheers,
Fraser.
Guards are really a series of Boolean equations, and the first that evaluates to true
Hi,
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?
head''
| Nil = Nothing
| Cons a _ = Just a
Thanks!
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe