Newline in Scheme (Racket)
Asked Answered
S

3

15

It is possible to have a new line when you write with "display" like

(display "exa \n mple")

But the problem is that there is no any code to have a new line in strings? Like:

"exa \n mple" 

Expected ->

exa
 mple

What I have:

exa \n mple

I couldn't find any information in racket-documentation or anywhere else.

Suppository answered 15/3, 2013 at 3:3 Comment(3)
The question is not clear, what do you need again?Jameson
hmmm this works in the racket interpreter on my mac terminal. Are you referring to Dr Racket's interpreter or in racket's interpreter?Bonitabonito
@JasonYeo , dr racket interpreter with racket dialectSuppository
S
13

If you need a way to add a newline between strings, then this will work:

(define s (string-append "exa " "\n" " mple"))
s
=> "exa \n mple"

(display s)
=> exa
    mple

In the above snippet, I'm using string-append for sticking together two strings with a newline in the middle. The resulting string s will have a newline in-between when you use it, for example by displaying it.

Obviously it will show up as "exa \n mple", you can't expect the string to jump from one line to the other unless you display it, print it, show it, write it, etc.

Scream answered 15/3, 2013 at 3:8 Comment(7)
Yes but when you work with strings, "\n" doesn't work anymoreSuppository
What do you mean "it doesn't work anymore"? it's working all right, notice that I'm using two backslashes for escapingJameson
Thus, you mean: If you enter this code: (string-append "bra" "\\n" "va"), you take "va" under the line of "bra" ? I'm using scheme with dialect racket and "\\n" doesn't start a new lineSuppository
I know that if you use (display) you will have always a new line with "\n" but my question is whether there is a way to add a new line directly in string. Suppose that i will use it to show in a GUI of scheme and therefore new line directly has to be added in string.Suppository
@Suppository that's precisely what my last attempt does: look, you just have to put a "\n" (without escaping it!) between two strings and append them, the resulting string, when displayed, or used will have a newline in-between.Jameson
now i'm okey :) thank you for your answer and sorry for unclear question!Suppository
@Suppository that's all right, it was fun getting to understand what you needed :)Jameson
B
17

Something like:

(display "line\nhola")

should do it.

Bonis answered 2/3, 2016 at 19:51 Comment(0)
S
13

If you need a way to add a newline between strings, then this will work:

(define s (string-append "exa " "\n" " mple"))
s
=> "exa \n mple"

(display s)
=> exa
    mple

In the above snippet, I'm using string-append for sticking together two strings with a newline in the middle. The resulting string s will have a newline in-between when you use it, for example by displaying it.

Obviously it will show up as "exa \n mple", you can't expect the string to jump from one line to the other unless you display it, print it, show it, write it, etc.

Scream answered 15/3, 2013 at 3:8 Comment(7)
Yes but when you work with strings, "\n" doesn't work anymoreSuppository
What do you mean "it doesn't work anymore"? it's working all right, notice that I'm using two backslashes for escapingJameson
Thus, you mean: If you enter this code: (string-append "bra" "\\n" "va"), you take "va" under the line of "bra" ? I'm using scheme with dialect racket and "\\n" doesn't start a new lineSuppository
I know that if you use (display) you will have always a new line with "\n" but my question is whether there is a way to add a new line directly in string. Suppose that i will use it to show in a GUI of scheme and therefore new line directly has to be added in string.Suppository
@Suppository that's precisely what my last attempt does: look, you just have to put a "\n" (without escaping it!) between two strings and append them, the resulting string, when displayed, or used will have a newline in-between.Jameson
now i'm okey :) thank you for your answer and sorry for unclear question!Suppository
@Suppository that's all right, it was fun getting to understand what you needed :)Jameson
T
8

For those that came here looking for how to do something similar to Java's println or Python's print that adds a newline at the end of the line, racket has a function called displayln that does just that. More complicated C-like format printing can be done in Racket with fprintf, where it looks like a '~n' gets interpreted as a newline.

Many of Racket's printing functions are documented here: https://docs.racket-lang.org/reference/Writing.html

Tingley answered 12/11, 2020 at 20:4 Comment(1)
println works too in Racket!Madelaine

© 2022 - 2024 — McMap. All rights reserved.