-
-
Save bonnefoa/8c8162540899ccebd12f to your computer and use it in GitHub Desktop.
List recursions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Control.Monad | |
import Control.Arrow | |
import System.IO.Unsafe | |
{-Without pattern matching-} | |
version1 :: IO() | |
version1 = pr [1..5] | |
where | |
pr :: (Show a) => [a] -> IO () | |
pr xs = unless (null xs) (print (head xs) >> pr (tail xs)) | |
{-With Control monad-} | |
version2 :: IO() | |
version2 = mapM_ print [1..5] | |
{-With Control monad and applicatives functor-} | |
version3 :: IO() | |
version3 = sequence_ (fmap print [1..5]) | |
{-With list comprehension-} | |
version4 :: IO() | |
version4 = sequence_ [print a | a <- [1..5] ] | |
{-With list monad and some wtf -} | |
{- Only those who bear the name of Simon can use unsafePerformIO -} | |
version5 :: IO() | |
version5 = when (all (<5) lst) (return lst >> return () ) | |
where lst = [1..5] >>= \a -> return (unsafePerformIO (print a >> return a) ) | |
{-Display of results-} | |
printer :: [IO()] | |
printer = map (show >>> ("version"++) >>> print) [1..5] | |
main :: IO() | |
main = mapM_ (\(a, b) -> a >> b) $ zip printer [version1, version2, | |
version3, version4, version5] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment