[Haskell-beginners] first number is a list

Mats Rauhala mats.rauhala at gmail.com
Mon Jun 27 14:20:18 CEST 2011


On 12:08 Mon 27 Jun     , Roelof Wobben wrote:
>  Sorry,  Now Im missing you. First you talked about head[x:-] = x and now head [1,2,3,4] is good. Roelof  Date: Mon, 27 Jun 2011 15:03:00 +0300

I wasn't entirely clear. The function declaration for head is:

 head (x:_) = x
 head _ = error "No elements"

or something similar. You can see from this that it uses pattern
matching to find the first element (x:_). _ means that the value is
disgarded. [1,2,3,4] is just syntactic sugar for 1 : 2 : 3 : 4 : [], so
pattern matching (x:_) finds 1 from the previous list.

You use the function head like I mentioned in the last email; `head
[1,2,3,4]`. Try firing up ghci and inputting that in it.


-- 
Mats Rauhala
MasseR
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110627/d68a306e/attachment.pgp>


More information about the Beginners mailing list