[Haskell-beginners] Are these soloutions all valid and a good use of Haskell

Roelof Wobben r.wobben at home.nl
Mon Nov 10 09:50:23 UTC 2014


I tried to solve the first problem of the 99 haskell problems on 3 
seperate ways.
The problem is that  I have to find the last item of a list.

The solutions I found with a little help of this mailinglist are :

last2::[a]-> Maybe a;
last2 [] = Nothing;
last2 ([x]) = Just x;
last2 (_:xs) = last2 xs

last3::[a]-> Maybe a;
last3 x
   | null x = Nothing
   | null xs = Just (head x)
   | otherwise = last3 (tail x)
   where xs = tail x

last4::[a]-> Maybe a;
last4 x = case x of
     [] -> Nothing ;
     [x] -> Just x ;
     (_:xs) -> last4 xs

main = print $ (last2 [] )

What do you experts think of the different ways ?


More information about the Beginners mailing list