unlines with fold

Real World Haskell, Exercise 10, p. 99: This was an easy one:

myUnlines :: [String] -> String
myUnlines xs = foldr (addLine) "" xs 
    where addLine c acc = acc ++ c ++ "\n"

It’s pretty much straight down the path of exactly what folds are meant for, though the inferred types still managed to surprise me, and my initial implementation was about twice this size.

Leave a Reply