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

10 lines
207 B
Haskell
Raw Normal View History

2024-05-24 15:56:10 -05:00
-- Simple recursive solution
myLast :: [a] -> a
myLast [] = error "Empty list"
myLast [s] = s
2024-05-24 17:06:42 -05:00
myLast (_ : tl) = myLast tl
2024-05-24 15:56:10 -05:00
-- Alternative Solution using fold
myLast' :: [a] -> a
myLast' = foldl1 (\_ x -> x)