Trouble with function with two clauses

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!

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@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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Fernando Rodriguez wrote:
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
You cannot use | as a general shortcut in function definitions as you try here. The stroke is instead used to express different branches selected by boolean expressions: headIfNotZero Nil = Nothing headIfNotZero (Cons a) | a == 0 = Nothing | otherwise = Just a In this definition, (a == 0) and (otherwise) are boolean expressions. GHCi, version 6.8.1: http://www.haskell.org/ghc/ :? for help Loading package base ... linking ... done. Prelude> otherwise True But in your definition of head'', Nil and (Cons a) are Patterns, not boolean expressions, so it doesnt work out. If you don't like to write out the name of the function you want to define over and over again, you can use an explicit case statement: head''' x = case x of Nil -> Nothing Cons a _ -> Just a This is sometimes easier to read, but in this case, I would use your head' definition. Tillmann

On 2008.01.09 18:15:33 +0000, Fernando Rodriguez
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!
Couple of things. Your head'' is trying to use pattern-matching, but guards (which are what you are actually using) require a Bool expression on the left hand side; guards just desugar into if-then-else clauses. In this case it'd look something like
head'' a = if Nil then Nothing else if Cons a _ then Just a else ???
This doesn't make sense. Nil is just Nil, it's not in Bool; the if can't do anything with that. Similarly, Cons a _ is just ConsCell and in the Show typeclass; it too is not Bool. If we turn it into pattern-matching:
head'' Nil = Nothing head'' Cons a _ = Just a
But this still doesn't work - one definition takes a single argument, and the other 3; Nil (1), vs [Cons, a, _] (3). So: head'' Nil = Nothing head'' (Cons a _) = Just a Parentheses force the Cons to be seen as a single argument. So I guess your final product would look like this, which is pretty much what you start out with.
data ConsCell a = Nil | Cons a (ConsCell a) deriving Show
head' :: ConsCell a -> Maybe a head' Nil = Nothing head' (Cons a _) = Just a
-- gwern Hackers Emerson EO SAS Majic CANSLO rail ABC CFD RSOC

On Wed, 9 Jan 2008, Fernando Rodriguez 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?
head'' | Nil = Nothing | Cons a _ = Just a
Maybe you want head''' x = case x of Nil -> Nothing Cons a _ -> Just a
participants (5)
-
Fernando Rodriguez
-
Fraser Wilson
-
gwern0@gmail.com
-
Henning Thielemann
-
Tillmann Rendel