99-haskell-problems/Problems 1-10/problem_1.hs

9 lines
208 B
Haskell

-- Simple recursive solution
myLast :: [a] -> a
myLast [] = error "Empty list"
myLast [s] = s
myLast (hd : tl) = myLast tl
-- Alternative Solution using fold
myLast' :: [a] -> a
myLast' = foldl1 (\_ x -> x)