User Tools

Site Tools


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

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
You could leave a comment if you were logged in.
codesnippets/accumulatingwithinmonadiccontextswithoutfolds.txt · Last modified: by 127.0.0.1

Except where otherwise noted, content on this wiki is licensed under the following license: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki