Skip to content

Instantly share code, notes, and snippets.

@myuon
Created January 19, 2014 03:04
Show Gist options
  • Save myuon/8499994 to your computer and use it in GitHub Desktop.
Save myuon/8499994 to your computer and use it in GitHub Desktop.
import Data.Functor.Free
import Data.Monoid
type CSV a = Free Monoid [a]
cell :: a -> CSV a
cell = unit . return
fromList :: [a] -> CSV a
fromList = unit
toList :: CSV a -> [a]
toList = counit
showCSV :: (Show a) => CSV a -> String
showCSV = rightAdjunct show' where
show' :: (Show a) => [a] -> String
show' [] = ""
show' (c:cs) = show c ++ "," ++ show' cs
main = do
putStrLn $ showCSV $ fromList [1..20]
-- #=> 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
putStrLn $ showCSV $ fmap (++ ["s", "s2", "s3"]) $ fromList ["aaa", "bbb", "ccc"]
-- #=> "aaa","bbb","ccc","s","s2","s3",
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment