[Haskell-cafe] xs not in scope

G Philip haskell-cafe at accounts.gphilip.in
Thu Nov 13 14:41:44 UTC 2014



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 <r.wobben at home.nl>:
> >
> >> 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 at 10:8-10:10Not in scope: xs
> >>
> >> How to solve this ?
> >>
> >> Roelof
> >>
> >>
> >> _______________________________________________
> >> Haskell-Cafe mailing list
> >> Haskell-Cafe at haskell.org
> >> http://www.haskell.org/mailman/listinfo/haskell-cafe
> > _______________________________________________
> > Haskell-Cafe mailing list
> > Haskell-Cafe at haskell.org
> > http://www.haskell.org/mailman/listinfo/haskell-cafe
> >
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe


More information about the Haskell-Cafe mailing list