Been messing around for about 20 minutes now trying to get the new line working however it always shows in GHCI as a single line.
Here is what I enter into GHCi:
displayFilm ("Skyfall",["Daniel Craig", "Judi Dench", "Ralph Fiennes"], 2012, ["Bill", "Olga", "Zoe", "Paula", "Megan", "Sam", "Wally"])
Here is what is printed:
"Skyfall----------\n Cast: Daniel Craig, Judi Dench, Ralph Fiennes\n Year: 2012\n Fans: 7\n"
displayList :: [String] -> String
displayList [] = ""
displayList [x] = x ++ "" ++ displayList []
displayList (x:xs) = x ++ ", " ++ displayList xs
displayFilm :: Film -> String
displayFilm (title, cast, year, fans) =
title ++ "----------" ++
"\n Cast: " ++ (displayList cast) ++
"\n Year: " ++ (show year) ++
"\n Fans: " ++ show (length fans) ++ "\n"
To print a string as it is, without escaping special characters, use:
putStr string
or
putStrLn string
if you want an extra newline at the end. In you case, you are probably looking for
putStr (displayFilm (....))
Why is this needed? In GHCi, if you evaluate an expression s the result will be printed as if running print s (unless it has type IO something -- forget about this special case). If e is a string, print escapes all the special characters and output the result. This is because print is meant to output a string whose syntax follows the one in Haskell expressions. For numbers, this is the usual decimal notation. For strings, we get quotes and escaped characters.
When you type an expression into GHC, it displays it using print. Calling print on a string shows its content but does not evaluate escape sequences:
> print "line1\nline"
"line1\nline2"
Note the quotes.
To display the string as you desire, use putStr or putStrLn (the latter will append a newline).
> putStr "line1\nline2"
line1
line2
Related
I've been looking through previously asked questions and I can't find an answer that solves my problem, although I thought at least one of them would. I'm simply trying to have a newline character added between my strings inside of a function. Whenever I add a "\n" to a string, it simply prints the "\n"
import Data.List
-- aRow takes number of columns as argument
-- The idea is to use this function with the number of columns as argument.
-- Example, if we want 3 columns, we'd say aRow 3, and get "+---+---+---+"
aRow :: Int -> String
aRow n = "+" ++ take (4*n) (intercalate "" (repeat "---+")) ++ "\n|" ++ take (4*n) (intercalate "" (repeat " |"))
This is the output I'm getting
"+---+---+---+---+\n| | | | |"
and I'd prefer
"+---+---+---+---+"
"| | | | |"
Where the lines are on separate lines (Also there's supposed to be 3 spaces between the vertical bars, ignore my formatting. I'm mainly trying to get the newline character working). Thank you.
https://stackoverflow.com/a/5944062/1848654:
If you just evaluate a string expression in ghci without using putStr or putStrLn, it will just call show on it, so for example the string "foo\n" will display as "foo\n" in ghci, but that does not change the fact that it's a string containing a newline and it will print that way, once you output it using putStr.
Long story short, you may want to use putStr as Haskell will default to using show on that string and that will display the \n plainly as it has done for you here.
Example:
import Data.List
main = putStrLn(aRow 4)
aRow :: Int -> String
aRow n = "+" ++ take (4*n) (intercalate "" (repeat "---+")) ++ "\n|" ++ take (4*n) (intercalate "" (repeat " |"))
I'm trying to create a show' function that will take a list of tuples and return a string that creates a new line after every tuple. So it would take [(x,y),(a,b),(c,d)] and return
x y
a b
c d
what I have so far in terms of code is
show' :: [(String,Int)] -> String
show' [] = ""
show' (x:xs) = (fst x) ++ " " ++ (show (snd x)) ++ " something that will create a newline in IO " ++ show' xs
Newlines in Haskell can be represented with "\n".
If you are still printing strings to the console that contain the character sequence \n, it probably means you are using print. Use putStr or putStrLn instead.
You don't want to use print because it internally uses show, which encodes the literal newline as the characters \ and n. print is more useful in debugging than in actual production code.
There is function unlines which joins lines and appends newline to each of them. So you can use something like
show' :: [(String, Int)] -> String
show' xs = unlines $ map (\(x,y) -> (show x) ++ " " ++ (show y)) xs
Use putStrLn to print the output otherwise you'll see '\n' instead of newline.
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 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 [...]
I have this code and instead of it printing out "\n", I want it to put the next string on a new line, but cannot seem to figure it out. Any pointers?
onSeparateLines :: [String] -> String
onSeparateLines [] = ""
onSeparateLines ( x:[] ) = x
onSeparateLines ( x:xs ) = x ++ "\n" ++ onSeparateLines xs
what I get is
"AAAA\nAAAA"
which should be:
"AAAA"
"AAAA"
The given function and your use of "\n" are correct, so the error must be elsewhere. Without knowing the details, I suspect that you are using (the equivalent of) print rather than putStr to print your string. Make sure that your string is not being shown before it is printed.
If this is in GHCi, be aware that values are printed using print, so
> onSeparateLines ["foo", "bar"]
will print the string and show escaped characters. You want
> putStrLn (onSeparateLines ["foo", "bar"])
instead.
How can I create a newline inside a String? Is it possible without using IO ()?
formatRow :: Car -> String
formatRow (a, d:ds, c, x:xs) = a ++ " | " ++ x ++ concat xs ++ " | " ++ show c ++ " | " ++ d ++ concat ds ++ (show '\n')
To create a string containing a newline, just write "\n".
If you run your program on Windows, it will automatically be converted to "\r\n".
Note that calling show on it will escape the newline (or any other meta-characters), so don't do foo ++ (show "\n") or foo ++ (show '\n') - just use foo ++ "\n".
Also note that if you just evaluate a string expression in GHCi without using putStr or putStrLn, it will just call show on it, so for example the string "foo\n" will display as "foo\n" in GHCi, but that does not change the fact that it's a string containing a newline and it will print that way, once you output it using putStr.