
Hello, I have to find the first number is a list. So first I tried : let mylast = last [1,2,3,4]mylast This one works but you have to give 2 commands. So i was thinking about using list compreshion. So I did : [x | x <- [1,2,3,4] , last ] But then I see these error : Could not match expected type "Bool" with actual type [a0] -> a0 in the expression last in stdn of a list compreshion last in the expression : [x | x <- [1,2,3,4] , last ] What is my thinking error ? Roelof

2011/6/27 Roelof Wobben
Hello,
I have to find the first number is a list.
So first I tried :
let mylast = last [1,2,3,4] mylast
I'm confused : You say have to find the first number of a list but this gives you the last.
[x | x <- [1,2,3,4] , last ]
But then I see these error : Could not match expected type "Bool" with actual type [a0] -> a0
It's quite clear: The compiler expects a function that'll return a Bool value, but you gave it one that takes a list and returns an element from the list. Additional terms in the list comprehention are meant to be filters on elements of the list, not functions that work on the list. David.

On 27 Jun 2011, at 12:48, David Virebayre wrote:
But then I see these error : Could not match expected type "Bool" with actual type [a0] -> a0
It's quite clear: The compiler expects a function that'll return a Bool value, but you gave it one that takes a list and returns an element from the list.
Actually it's saying it expects a bool value, no function at all. Bob

I'm sorry, I'm not entirely sure what you're trying to do. Are you trying to find the first number _in_ a list? That could be done with for example pattern matching: head (x:_) = x head _ = error "Empty list" The guards in list comprehension requires booleans, you gave it a function that takes a list and returns an item of the same time. last :: [a] -> a -- Mats Rauhala MasseR

Oke, I'm not sure I understand you right. Let's say I have this list [1,2,3,4]How must I use head now ? Roelof Date: Mon, 27 Jun 2011 14:50:14 +0300 From: mats.rauhala@gmail.com To: beginners@haskell.org Subject: Re: [Haskell-beginners] first number is a list I'm sorry, I'm not entirely sure what you're trying to do. Are you trying to find the first number _in_ a list? That could be done with for example pattern matching: head (x:_) = x head _ = error "Empty list" The guards in list comprehension requires booleans, you gave it a function that takes a list and returns an item of the same time. last :: [a] -> a -- Mats Rauhala MasseR _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

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 From: mats.rauhala@gmail.com To: beginners@haskell.org Subject: Re: [Haskell-beginners] first number is a list On 11:55 Mon 27 Jun , Roelof Wobben wrote:
Oke, I'm not sure I understand you right. Let's say I have this list [1,2,3,4]How must I use head now ? Roelof Date: Mon, 27 Jun 2011 14:50:14 +0300
head [1,2,3,4] -- Returns 1 -- Mats Rauhala MasseR _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

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

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@gmail.com To: beginners@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@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On 12:24 Mon 27 Jun , Roelof Wobben wrote:
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]
tail returns the list except for the head. So for example the list [1,2,3,4] becomes [2,3,4]. Using last on that would return 4, not 3. However the function init returns the list except for the last item, so using init on [1,2,3,4] would return [1,2,3] and then using last on that would return the 3 you wanted. So for example: ghci> last $ init [1,2,3,4] 3 But Data.List exports a function `find :: (a -> Bool) -> [a] -> Maybe a`, which you can use to find 3. ghci> find (== 3) [1,2,3,4] Just 3 Another way could be using head and dropWhile: ghci> head $ dropWhile (/= 3) [1,2,3,4] 3 -- Mats Rauhala MasseR
participants (4)
-
David Virebayre
-
Mats Rauhala
-
Roelof Wobben
-
Thomas Davie