99 Elm Problems/Lists/Problem 1/Find the last element of a list/Solutions

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Solution 1: Recursive search for the last element

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

Solution 2: Reverse and take the head

myLast : List a -> Maybe a
myLast list = 
   List.reverse list |> List.head