99 Elm Problems/Lists/Problem 1: Find the last element of a list/Solutions
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 -> myLast c
Solution 2: Reverse and take the head
myLast : List a -> Maybe a
myLast list =
List.reverse list |> List.head