codesnippets:accumulatingwithinmonadiccontextswithoutfolds
Accumulating within monadic contexts without folds
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-} {-# HLINT ignore "Use void" #-} {-# HLINT ignore "Use map" #-} {-# HLINT ignore "Use foldl" #-} main :: IO () main = do print (f1a [3, 2, 6, 8]) f1b [3, 2, 6, 8] f1c [3, 2, 6, 8] >>= print >> return () print (f2a [] [3, 2, 6, 8]) f2b [3, 2, 6, 8] f2c [] [3, 2, 6, 8] >> return () f1a :: [Int] -> [Int] f1a [] = [] f1a (n:rln) = (n + 1) : f1a rln f1b :: [Int] -> IO () f1b ln = f1' ln >>= print where f1' :: [Int] -> IO [Int] f1' [] = return [] f1' (n:rln) = f1' rln >>= (\r -> return ((n + 1) : r)) f1c :: [Int] -> IO [Int] f1c [] = return [] f1c (n:rln) = f1c rln >>= (\r -> return ((n + 1) : r)) f2a :: [Int] -> [Int] -> [Int] f2a lnAccu [] = lnAccu f2a lnAccu (n:rln) = f2a (n + 1 : lnAccu) rln f2b :: [Int] -> IO () f2b ln = f2b' [] ln >>= print where f2b' :: [Int] -> [Int] -> IO [Int] f2b' lnAccu [] = return lnAccu f2b' lnAccu (n:rln) = f2b' ((n + 1) : lnAccu) rln f2c :: [Int] -> [Int] -> IO [Int] f2c lnAccu [] = print lnAccu >> return lnAccu f2c lnAccu (n:rln) = f2c ((n + 1) : lnAccu) rln
✎
You could leave a comment if you were logged in.
codesnippets/accumulatingwithinmonadiccontextswithoutfolds.txt · Last modified: by 127.0.0.1
