跳轉到內容

99 個 Elm 問題/問題 2/解決方法

來自維基文庫,開放世界中的開放書籍

解決方法 1:遞迴搜尋倒數第二個元素

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

解決方法 2:函式組合

import List exposing (reverse, drop, head)
penultimate =
    (reverse >> drop 1 >> head)
華夏公益教科書