summaryrefslogtreecommitdiff
path: root/testsuite/tests/cpranal/should_compile/T18401.hs
diff options
context:
space:
mode:
Diffstat (limited to 'testsuite/tests/cpranal/should_compile/T18401.hs')
-rw-r--r--testsuite/tests/cpranal/should_compile/T18401.hs20
1 files changed, 20 insertions, 0 deletions
diff --git a/testsuite/tests/cpranal/should_compile/T18401.hs b/testsuite/tests/cpranal/should_compile/T18401.hs
new file mode 100644
index 0000000000..c850d9a7e0
--- /dev/null
+++ b/testsuite/tests/cpranal/should_compile/T18401.hs
@@ -0,0 +1,20 @@
+{-# OPTIONS_GHC -O2 -fforce-recomp -dno-typeable-binds #-}
+
+module T18401 where
+
+-- | A safe version of `init`.
+-- @safeInit [] = Nothing@
+-- @safeInit xs = Just $ init xs@
+safeInit :: [a] -> Maybe [a]
+safeInit xs = case si xs of
+ (False, _) -> Nothing
+ (_, ys) -> Just ys
+
+si :: [a] -> (Bool, [a])
+si xs0 = foldr go stop xs0 Nothing
+ where
+ stop Nothing = (False, [])
+ stop _ = (True, [])
+ go x r Nothing = (True, snd (r (Just x)))
+ go x r (Just p) = (True, p : snd (r (Just x)))
+