< 99 Elm Problems < Problem 2

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