summaryrefslogtreecommitdiff
path: root/p2.hs
blob: 60693efe2a5b18dbac29d8d6ab60e0c9b1e2af27 (plain)
1
2
3
4
5
6
7
8
9
10
11
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)