
Hello, Im trying to find several answers to problem1 of the 99 haskell problems. Find the last item in a list. Now im trying to find a guarded solution. So far I have this: last3::[a]-> Maybe a; last3 a | [] = Nothing | ([a]) = Just a | (_:xs) = last3 xs But I see this error messages : src/Main.hs@10:8-10:10Not in scope: xs How to solve this ? Roelof

Hi Roelof,
the problem is, you cannot pattern-match within guards. Guards take
predicates.
On your example: "last3 a" pattern-matches the first argument of last3
into a. You may now use a in all further statements. If, however, you
want to do pattern matching, it would look like this:
last3::[a]-> Maybe a;
last3 [] = Nothing
last3 ([a]) = Just a
last3 (_:xs) = last3 xs
Notice that the last case will never be executed, as the matching is
complete with the first two case.
Max
Am Sun, 09 Nov 2014 17:38:26 +0100
schrieb Roelof Wobben
Hello,
Im trying to find several answers to problem1 of the 99 haskell problems. Find the last item in a list.
Now im trying to find a guarded solution. So far I have this:
last3::[a]-> Maybe a; last3 a | [] = Nothing | ([a]) = Just a | (_:xs) = last3 xs
But I see this error messages :
src/Main.hs@10:8-10:10Not in scope: xs
How to solve this ?
Roelof
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Oke, So this cannot be done with guards ? Roelof Max Voit schreef op 9-11-2014 18:04:
Hi Roelof,
the problem is, you cannot pattern-match within guards. Guards take predicates.
On your example: "last3 a" pattern-matches the first argument of last3 into a. You may now use a in all further statements. If, however, you want to do pattern matching, it would look like this:
last3::[a]-> Maybe a; last3 [] = Nothing last3 ([a]) = Just a last3 (_:xs) = last3 xs
Notice that the last case will never be executed, as the matching is complete with the first two case.
Max
Am Sun, 09 Nov 2014 17:38:26 +0100 schrieb Roelof Wobben
: Hello,
Im trying to find several answers to problem1 of the 99 haskell problems. Find the last item in a list.
Now im trying to find a guarded solution. So far I have this:
last3::[a]-> Maybe a; last3 a | [] = Nothing | ([a]) = Just a | (_:xs) = last3 xs
But I see this error messages :
src/Main.hs@10:8-10:10Not in scope: xs
How to solve this ?
Roelof
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

You could use pattern guards. last3 :: [a] -> Maybe a; last3 a | [] <- a = Nothing ... On 09/11/14 12:10, Roelof Wobben wrote:
Oke,
So this cannot be done with guards ?
Roelof
Max Voit schreef op 9-11-2014 18:04:
Hi Roelof,
the problem is, you cannot pattern-match within guards. Guards take predicates.
On your example: "last3 a" pattern-matches the first argument of last3 into a. You may now use a in all further statements. If, however, you want to do pattern matching, it would look like this:
last3::[a]-> Maybe a; last3 [] = Nothing last3 ([a]) = Just a last3 (_:xs) = last3 xs
Notice that the last case will never be executed, as the matching is complete with the first two case.
Max
Am Sun, 09 Nov 2014 17:38:26 +0100 schrieb Roelof Wobben
: Hello,
Im trying to find several answers to problem1 of the 99 haskell problems. Find the last item in a list.
Now im trying to find a guarded solution. So far I have this:
last3::[a]-> Maybe a; last3 a | [] = Nothing | ([a]) = Just a | (_:xs) = last3 xs
But I see this error messages :
src/Main.hs@10:8-10:10Not in scope: xs
How to solve this ?
Roelof
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe .

On Sun, Nov 9, 2014, at 06:10 PM, Roelof Wobben wrote:
So this cannot be done with guards ?
Here is one way to do this "with guards": last3 :: [a] -> Maybe a; last3 [] = Nothing last3 lst@(x:xs) | (null xs) = Just x | otherwise = last3 xs Regards, Philip
Roelof
Max Voit schreef op 9-11-2014 18:04:
Hi Roelof,
the problem is, you cannot pattern-match within guards. Guards take predicates.
On your example: "last3 a" pattern-matches the first argument of last3 into a. You may now use a in all further statements. If, however, you want to do pattern matching, it would look like this:
last3::[a]-> Maybe a; last3 [] = Nothing last3 ([a]) = Just a last3 (_:xs) = last3 xs
Notice that the last case will never be executed, as the matching is complete with the first two case.
Max
Am Sun, 09 Nov 2014 17:38:26 +0100 schrieb Roelof Wobben
: Hello,
Im trying to find several answers to problem1 of the 99 haskell problems. Find the last item in a list.
Now im trying to find a guarded solution. So far I have this:
last3::[a]-> Maybe a; last3 a | [] = Nothing | ([a]) = Just a | (_:xs) = last3 xs
But I see this error messages :
src/Main.hs@10:8-10:10Not in scope: xs
How to solve this ?
Roelof
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Mon, Nov 10, 2014 at 12:04 AM, Max Voit
last3::[a]-> Maybe a; last3 [] = Nothing last3 ([a]) = Just a last3 (_:xs) = last3 xs
Notice that the last case will never be executed, as the matching is complete with the first two case.
Lists are tricky because of the special syntax. The first case is [] or Nil, the second is [a] which is equivalent to (a:[]). Hence there's a remaining case of a Cons cell (:) that's NOT followed by Nil ([]), which is the third case. -- Kim-Ee
participants (5)
-
G Philip
-
Kim-Ee Yeoh
-
Max Voit
-
Roelof Wobben
-
Roman Cheplyaka