Question on (x:xs) form

Eg for a definition of reverse: reverse' :: [a] -> [a] reverse' [] = [] reverse' (x:xs) = reverse' xs ++ [x] In the last line of the definition, x is an element in the list (the first element) and xs represents the remainder of the list. so if list was [1,2,3] then x is 1 and xs is [2,3] Why are the brackets required? And what do they signify? Eg reverse' x:xs = reverse' xs ++ [x] results in a parse error.

On Mon, Dec 23, 2013 at 9:57 AM, Angus Comber
Why are the brackets required? And what do they signify? Eg reverse' x:xs = reverse' xs ++ [x] results in a parse error.
If you leave them off then it's parsed as three parameters instead of a pattern as a single parameter. But that also leads to a parse error since you can't use a bare operator that way. Which is "weird", but you can get into inconsistent parses if you try to guess that the parentheses are needed: should it be `(x:xs)` a pattern, or should it be `x (:) xs` 3 patterns with an infix constructor as the second? -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

On Mon, Dec 23, 2013 at 9:57 PM, Angus Comber
Why are the brackets required? And what do they signify?
Eg reverse' x:xs = reverse' xs ++ [x] results in a parse error.
So a number of Haskell-specific themes are relevant here, especially if you come from a Lisp background: * types (but you knew that already!) * pattern-matching & exhaustivity of matches * operator infix notation * associativity precedence * special notation for lists It's a good idea to watch out for all of the above when learning the language. It helps to have some google-able names when the situation is ripe for drilling deep into these topics. To elaborate a bit, Special List Notation: lists use (:) and [x]. But this syntax is baked specially into the language definition! If you define your own list, it'll typically look like data List a = Nil | Cons a (List a) -- note the parens! The alternative GADT-ish style may make this clearer: data List a where Nil :: List a Cons :: a -> List a -> List a Nil and Cons are data constructor *functions*, and because and only because they are so, do they get to have Capitalized Names. <------- this is another big WTF that trips up many, many learners. Especially with code like: newtype X a = X a -- whaaaa????? (It's ok, you'll get it soon enough.) Back to reverse'. With our user-defined List a, reverse' will look like reverse' Nil = reverse' (Cons x xs) = ... -- again note the parens! (There is however, *NO* difference in the semantics. The code just looks different but the compiler doesn't care.) Note how the pattern-matching includes every single case of the sum-type; here both of them: Nil and Cons. This is Generally A Good Thing! -- Kim-Ee

Hi Angus,
you need to put the parentheses around the pattern x:xs because the
application reverse' has priority over (:) , so the statement is the
same as
(reverse' x):xs = reverse' xs ++ [x]
and results the error.
-- Denis.
On Mon, Dec 23, 2013 at 12:57 PM, Angus Comber
Eg for a definition of reverse:
reverse' :: [a] -> [a] reverse' [] = [] reverse' (x:xs) = reverse' xs ++ [x]
In the last line of the definition, x is an element in the list (the first element) and xs represents the remainder of the list.
so if list was [1,2,3] then x is 1 and xs is [2,3]
Why are the brackets required? And what do they signify?
Eg reverse' x:xs = reverse' xs ++ [x] results in a parse error.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Am 23.12.2013 15:57, schrieb Angus Comber:
Why are the brackets required? And what do they signify?
Eg reverse' x:xs = reverse' xs ++ [x] results in a parse error.
I just want to add that "prefix application"/juxtaposition binds stronger than "infix application" and that therefore spaces should be put around infix symbols (like ":") to stress this fact: reverse x:xs is the same as reverse x : xs and both are parsed as (reverse x) : xs which is different from reverse (x : xs) although "x:xs" almost looks like "(x:xs)" Cheers Christian
participants (5)
-
Angus Comber
-
Brandon Allbery
-
Christian Maeder
-
Denis Albuquerque
-
Kim-Ee Yeoh