mark.piffer
2022-2-1 09:59:02

Is there a way to write string literals “concatenable”, i.e. “abcd” “efgh” to have a single literal “abcdefgh”? And are there multiline strings like in Python?


soegaard2
2022-2-1 10:01:31

Multiline strings are called “here strings” in Racket. Example:

#<<END A line. A second line. END

You can choose a different word than END.


soegaard2
2022-2-1 10:02:13

I am not sure what you mean by “contenable string literals”.


soegaard2
2022-2-1 10:04:14

The function ~a can be uses as an easy way to concatenate strings and to convert numbers to strings.

(~a "There are " 42 "apples.")


soegaard2
2022-2-1 10:05:04

If you use

`#lang at-exp racket`

you can write:

@~a{There are @(+ 41 1) apples.}


soegaard2
2022-2-1 10:05:52

rokitna
2022-2-1 10:21:58

Strings delimited by " can have newlines in them, too


mark.piffer
2022-2-1 10:31:27

Concatenable (or better: automatically concatenated) strings can be found e.g. in C, where printf("abcd" "efgh"); concatenates adjacent string literals during parsing


mark.piffer
2022-2-1 10:32:12

so (~a comes pretty close to that


soegaard2
2022-2-1 10:35:01

In C arguments are separated by comma. So it is not a problem to “foo” “bar” into “foobar”. In Racket arguments are separated by white space, so that’s not an option. However, if you have a long string and want to split it over two lines, you can write as the last character on the line and continue at the next line.


soegaard2
2022-2-1 10:35:11

The result is “foobar”.


laurent.orseau
2022-2-1 10:55:01

You can also use normal strings by the way: (define str "This is a multiline string") but it may be considered bad style due to breaking the <https://github.com/google/google-java-format/wiki/The-Rectangle-Rule|’rectangular rule’>. A slightly better style maybe(?): (define str "\ This is a multiline string")


laurent.orseau
2022-2-1 10:55:45

or string-append?


sorawee
2022-2-1 13:16:09

Yeah, I would suggest using ~a or string-append:

(define test (~a "hello" "world"))


sschwarzer
2022-2-1 13:54:38

I used string-append for that. I didn’t know that the shorter ~a can do the same. Nice! :slightly_smiling_face:


spdegabrielle
2022-2-1 18:26:18

sorawee
2022-2-2 00:09:42

That probably won’t work though. The arguments to ~a by @ reader will be something like '("hello" "\n" "world"). So you need something other than ~a to filter out \n (and also to decode literal \n to newline, etc.).