Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write multiline strings in Haskell?

Tags:

haskell

Let's say I have this string literal with line breaks:

file :: String file = "the first line\nthe second line\nthe third line" 

Is there any way to write it like this?

file :: String file = "the first line         the second line         the third line" 

The attempt just above leads to this error:

factor.hs:58:33:     lexical error in string/character literal at character '\n' Failed, modules loaded: none. 
like image 904
liubiantao Avatar asked Apr 07 '14 17:04

liubiantao


People also ask

How do you write multiline strings?

Use triple quotes to create a multiline string It is the simplest method to let a long string split into different lines. You will need to enclose it with a pair of Triple quotes, one at the start and second in the end. Anything inside the enclosing Triple quotes will become part of one multiline string.

Can a string be multiple lines?

Raw StringsThey can span multiple lines without concatenation and they don't use escaped sequences. You can use backslashes or double quotes directly.

Which characters are used for multiline strings?

Three single quotes, three double quotes, brackets, and backslash can be used to create multiline strings.

What starts and ends a multiline string?

A multiline string in Python begins and ends with either three single quotes or three double quotes. Any quotes, tabs, or newlines in between the “triple quotes” are considered part of the string.


1 Answers

You can write multiline strings like so

x = "This is some text which we escape \       \   and unescape to keep writing" 

Which prints as

"This is some text which we escape   and unescape to keep writing" 

If you want this to print as two lines

x = "This is some text which we escape \n\       \   and unescape to keep writing" 

Which prints as

This is some text which we escape     and unescape to keep writing 
like image 169
Daniel Gratzer Avatar answered Dec 30 '22 21:12

Daniel Gratzer