From 0135b3ac0d41dda2a4c210e7c3989f20b672be5d Mon Sep 17 00:00:00 2001 From: Joe Zhao Date: Thu, 29 Jan 2015 16:29:05 +0800 Subject: p1+ p2 --- p2.hs | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 p2.hs (limited to 'p2.hs') diff --git a/p2.hs b/p2.hs new file mode 100644 index 0000000..60693ef --- /dev/null +++ b/p2.hs @@ -0,0 +1,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) -- cgit v1.2.3-70-g09d2