Jump to content

99 Elm Problems/Problem 1/Solutions

From Wikibooks, open books for an open world

Solution 1: Recursive search for the last element

myLast list = 
  case list of
    [] -> Nothing
    [a] -> Just a
    b::c -> myLast c

Solution 2: Reverse and take the head

myLast = List.reverse >> List.head

Solution 3: Using List.foldl

myLast = List.foldl (\x _ -> Just x) Nothing