< 99 Elm Problems < Problem 1

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
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.