From a5ce95f40188c4525bc98307d09015a5dca93bb3 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Sun, 25 Sep 2022 18:00:47 +0300 Subject: Refactor Lib.hs --- src/Utils.hs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/Utils.hs') diff --git a/src/Utils.hs b/src/Utils.hs index fe0a9c2..e2c182b 100644 --- a/src/Utils.hs +++ b/src/Utils.hs @@ -113,3 +113,17 @@ evenElems (_:xs) = oddElems xs throwL :: String -> LContext a throwL s = throwError $ LException s + +asPairsM :: [a] -> LContext [(a, a)] +asPairsM [] = return [] +asPairsM (a:b:rest) = do + restPaired <- asPairsM rest + return $ (a, b) : restPaired +asPairsM _ = throwL "odd number of elements to pair up" + +asPairs :: [a] -> [(a, a)] +asPairs [] = [] +asPairs (a:b:rest) = + let restPaired = asPairs rest + in (a, b) : restPaired +asPairs _ = error "odd number of elements to pair up" -- cgit v1.3