99-haskell-problems/Problems 01-10/problem_01.hs

9 lines
207 B
Haskell

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