0

I'm trying to write a program that writes the pound sign '£' in Haskell, but it outputs '\163' whenever I try to use it. I'm guessing that this is some alphanumeric code, but how do I get it to display what I want it to? I'm writing to the console, when calling a function that returns '£'.

Thank you.

Jonathan
  • 11
  • 1
  • What code are you using? Is your terminal capable of displaying non-ASCII characters? – chepner Jan 25 '21 at 18:39
  • `pennies2pounds :: Int -> String` `pennies2pounds pennies = "£" ++ show pounds ++ "." ++ show pence` `where` `(pounds, pence) = divMod pennies 100` I'm using ghci to load the program in command prompt on Windows, and then calling pennies2pounds with integers. – Jonathan Jan 25 '21 at 18:44
  • 1
    You might be `show`ing or `print`ing strings, which adds extra quotes and escapes all non-ASCII characters. In GHCi, if you just want to output a string, use `putStrLn (the string-returning expression)`. E.g. `putStrLn (pennies2pounds 100)` – chi Jan 25 '21 at 18:46
  • Thanks, that solved it. I never knew `print` and `show` escapes non-ASCII characters. – Jonathan Jan 25 '21 at 18:48
  • `print` calls `show`, and the latter always outputs an ASCII string, representing values as done by Haskell's syntax. So, `show "abc" == "\"abc\""`, for instance. – chi Jan 25 '21 at 18:59

1 Answers1

1

This was solved by using putStrLn, because print and show do not allow for non-ASCII characters to be shown.

Jonathan
  • 11
  • 1
  • 1
    More to the point, `show` and `print` take a value which is an instance of `Show` and returns or outputs a Haskell expression that evaluates to that value, in this case a `Char` whose `ord` is 163. – David Fox Jan 25 '21 at 20:14