[Haskell-beginners] first number is a list

Roelof Wobben rwobben at hotmail.com
Mon Jun 27 14:24:21 CEST 2011



 Oke,  Now figure out how I can find the 3 in the list [1,2,3,4] I was thinking about using tail [1,2,3,4] and after that use last [1,2,3] So something like last [ tail [1,2,3,4] Or : let outcome = tail [1,2,3,4]let outcome2 = last [outcome] Roelof Date: Mon, 27 Jun 2011 15:20:18 +0300
From: mats.rauhala at gmail.com
To: beginners at haskell.org
Subject: Re: [Haskell-beginners] first number is a list

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

_______________________________________________
Beginners mailing list
Beginners at haskell.org
http://www.haskell.org/mailman/listinfo/beginners 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110627/63582477/attachment.htm>


More information about the Beginners mailing list