summaryrefslogtreecommitdiff
path: root/p2.hs
diff options
context:
space:
mode:
authorJoe Zhao <ztuowen@gmail.com>2015-01-29 16:29:05 +0800
committerJoe Zhao <ztuowen@gmail.com>2015-01-29 16:29:05 +0800
commit0135b3ac0d41dda2a4c210e7c3989f20b672be5d (patch)
tree7284971efac168e9c704ec74e50cf4ce66df6916 /p2.hs
parentedcc6ba8ca9d6b883b59548eb8bb5e312c73f43b (diff)
downloadh99-0135b3ac0d41dda2a4c210e7c3989f20b672be5d.tar.gz
h99-0135b3ac0d41dda2a4c210e7c3989f20b672be5d.tar.bz2
h99-0135b3ac0d41dda2a4c210e7c3989f20b672be5d.zip
p1+ p2
Diffstat (limited to 'p2.hs')
-rw-r--r--p2.hs11
1 files changed, 11 insertions, 0 deletions
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)