Skip to content

Instantly share code, notes, and snippets.

@bonnefoa

bonnefoa/Main.hs Secret

Created June 23, 2011 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bonnefoa/8c8162540899ccebd12f to your computer and use it in GitHub Desktop.
Save bonnefoa/8c8162540899ccebd12f to your computer and use it in GitHub Desktop.
List recursions
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