ajanakos
ajanakos

Reputation: 119

Haskell + output String as type String

I've written a basic recursive function:

bibliography_rec :: [(String, String, Int)] -> String
bibliography_rec [] = ""
bibliography_rec (x:xs) = (citeBook x) ++ "\n" ++ (bibliography_rec xs)

citeBook simply reformats the tuple into a String.

When run with this input:

ghci> bibliography_rec [("Herman Melville", "Moby Dick", 1851),("Georgy Poo", "Alex Janakos", 1666)]

It produces:

"Moby Dick (Herman Melville, 1851)\nAlex Janakos (Georgy Poo, 1666)\n"

I need line by line printing so I used this:

bibliography_rec (x:xs) = putStr ((citeBook x) ++ "\n" ++ (bibliography_rec xs))

My problem is my output NEEDS to be of type String NOT IO ()

I've been stuck on this for way too long so any help is great!

Upvotes: 1

Views: 232

Answers (1)

luqui
luqui

Reputation: 60543

Looks like you're already there, you just need to putStrLn the string instead of printing it (which is what ghci does by default). print runs its argument through show first, so it will quote the escape characters like "\n".

ghci> putStrLn $ bibliography_rec [...]

Upvotes: 5

Related Questions