简体   繁体   中英

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!

Looks like you're already there, you just need to putStrLn the string instead of print ing 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 [...]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM