import Data.Foldable as F myButLast :: Foldable f => f a -> a myButLast = fst . F.foldl (\(a,b) x -> (b,x)) (err1, err2) where err1 = error "Empty list" err2 = error "Not enough elements" mySafeButLast :: Foldable f => f a -> Maybe a mySafeButLast = fst . F.foldl (\(a,b) x -> (b,Just x)) (Nothing, Nothing)