Newline is printing \n rather than the usual

Viewed 81

This is a "continuation" from this post: How do I handle this data type

So now I have to print a "table" of numbers according to Numbers type in this format:

Positive Numbers:

1

4

16

Negative Numbers:

-3

-69

Null Numbers:

0

0

So I made this function:

printNum :: Numbers -> String
printNum (Numbers po ne nn)=  "Positive Numbers: \n " ++ numP po ++
                              " \n Negative Numbers: \n " ++ numN ne ++
                              " \n Null Numbers: \n " ++ numZ nn
  where
      numP (p:ps) = show p ++ " \n " ++ numP ps
      numP [] = []
      numN (n:ns) = show n ++ " \n " ++ numN ns
      numN [] = []
      numZ (z:zs) = show z ++ " \n " ++ numZ zs
      numZ [] = []

But the function is printing like this: "Positive Numbers: \n 1 \n 4 \n 16 \n \n Negative Numbers: \n -3 \n -69 \n \n Null Numbers: \n 0 \n 0 \n "

1 Answers

In GHCi, if you enter an expresssion it will evaluate that expression, and if the outcome type is not unit (()), it will print to use show to present the result of that expression. For a String, it will thus write a String literal to present this.

You can work with putStrLn :: String -> IO () to print the content of the string, not as a string literal, so:

putStrLn (printNum numex)
Related