How does \n work in Char lists in haskell
right now I have a String equal to "...\n...\n..."
. In my code I want to write this as a list (like ['a','b','c']
), but how would this work with the \n? I checked in ghci if string == ['.','.','.','.','.','.','.','.','.']
and it said no, so does anyone know how I would write the \n's in a Char list, thank you.
Solution 1:
A String
is a list of Char
acters, so "foo"
and ['f', 'o', 'o']
are exactly the same.
For a new line character '\n'
[wiki] you can escape this, so your string "...\n...\n..."
is equivalent to:
['.', '.', '.', '\n', '.', '.', '.', '\n', '.', '.', '.']
Here '\n'
is a single character, not two: it maps to an ASCII character with codepoint 0a
as hexadecimal value (10
as decimal value). The compiler thus sees \n
and replaces that with a single character.
You can thus for example filter with filter ('\n' /=) some_string
to filter out new line characters from a String
.