summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--p1.hs4
-rw-r--r--p2.hs11
2 files changed, 15 insertions, 0 deletions
diff --git a/p1.hs b/p1.hs
index 3afd512..f0216c2 100644
--- a/p1.hs
+++ b/p1.hs
@@ -3,3 +3,7 @@ myLast :: [a] -> a
myLast [] = error "Empty List"
myLast [x] = x
myLast (x:xs) = myLast xs
+
+myLast' = foldl1 (flip const)
+
+myLast'' = foldl1 (curry snd)
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)