summaryrefslogtreecommitdiff
path: root/H2.hs
diff options
context:
space:
mode:
Diffstat (limited to 'H2.hs')
-rw-r--r--H2.hs11
1 files changed, 11 insertions, 0 deletions
diff --git a/H2.hs b/H2.hs
new file mode 100644
index 0000000..60693ef
--- /dev/null
+++ b/H2.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)