codesnippets:accumulatingwithinmonadiccontextswithoutfolds
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| codesnippets:accumulatingwithinmonadiccontextswithoutfolds [2022/05/28 16:08] – created f2b216 | codesnippets:accumulatingwithinmonadiccontextswithoutfolds [2025/10/08 00:48] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ~~ DISCUSSION ~~ | ||
| - | |||
| ====== Accumulating within monadic contexts without folds ====== | ====== Accumulating within monadic contexts without folds ====== | ||
| + | * <code Haskell> | ||
| + | {-# 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 | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== ✎ ===== | ||
| + | ~~DISCUSSION~~ | ||
codesnippets/accumulatingwithinmonadiccontextswithoutfolds.1653746910.txt.gz · Last modified: (external edit)
