4.1 Strings TBoS p.34,75-83 SD
Strings begin and end with double quotes. Strings can be created from atoms (strings, symbols and numbers)
by the str function. A unit string is a string consisting of a single character; "w", "e", "4" etc.
The fast 2~place concatenation operator for strings is 'cn'. '@s' is a polyadic concatenation operator.
'(pos s n)' selects the nth unit string from s starting from zero. 'tlstr' returns all but the first element of
a string.
"hello world"
"hello world"
(pos "hello world" 6)
"w"
(@s "1" "2" "3")
"123"
(tlstr "123")
"23" |
The function 'make~string' is polyadic and permits embedded directives in its first argument.
A directive is either ~A, ~S, ~R, ~%.
~A is a directive to insert the corresponding argument as part of the string ignoring any double quotes.
~S is a directive to insert the corresponding argument as part of the string, not ignoring double quotes.
~R is a directive to insert the corresponding argument as part of the string, printing list structures with round brackets.
~% is a directive to format a new line.
Here is a script.
(make~string "a string~%")
"a string
"
(make~string "hello world~%")
"hello world
"
(make~string "~A says, hello world~%" "Fred")
"Fred says, hello world
"
(make~string "~S says, hello world~%" "Fred")
""Fred" says, hello world
"
(make~string "~S say, hello world~%" [Bill and Ben])
"[Bill and Ben] say, hello world
"
(make~string "~R say, hello world~%" [Bill and Ben])
"(Bill and Ben) say, hello world
" |
c#n; where n is a natural number, embeds a unit string corresponding to code point n into a string.
Shen supports code points 0~127 (the ASCII set) and may depending on the platform, support others. The functions
n~>string and string~>n map a code point to a unit string and a unit string to a code point.
(n~>string 56)
"8"
"
(string~>n "y")
"121"
"hello worldc#33;"
"hello world!" |
Note '($ 123)' is read by the reader as ["1" "2" "3"]. $ is not a function however. The function 'explode' which
produces the same result is a function. |