Jump to content

99 Elm Problems/Problem 2/Solutions

From Wikibooks, open books for an open world

Solution 1: Recursive search for the next to last element

penultimate list =
  case list of
    [] -> Nothing
    next :: [_] -> Just next
    _ :: tail -> penultimate tail

Solution 2: Functional composition

import List exposing (reverse, drop, head)
penultimate =
    (reverse >> drop 1 >> head)