[Haskell-cafe] Trouble with function with two clauses

Fraser Wilson 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 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
On Jan 9, 2008 7:15 PM, Fernando Rodriguez < frr149 at easyjob.net> wrote:

>
> 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 at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20080109/0bf535fb/attachment.htm


More information about the Haskell-Cafe mailing list