Strings


 

 

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.

1. Introduction

2. License

3. History

4. The Core Language

4.1 Base Types
4.1.1 Symbols
4.1.2 Strings
4.1.3 Numbers
4.2 Function Applications
4.3 The Top Level
4.4 Arithmetic
4.5 Comments

4.6 Sequences

4.6.1 Lists
4.6.2 Tuples
4.6.3 Vectors

4.7 lambda and let
4.8 Global Assignments
4.9 Higher Order Functions
4.10 Lazy Evaluation
4.11 I/O
4.12 Loading Files
4.13 Streams
4.14 Exceptions
4.15 Hashing
4.16 Property Lists
4.17 Eval

5 Defining Functions

5.1 Partial Functions
5.2 List Handling Functions
5.3 String Handling Functions
5.4 Tuple Handling Functions
5.5 Vector Handling Functions
5.6 Guards
5.7 Backtracking
5.8 Writing in Kλ
5.9 Macros

6. Packages

7. Shen-YACC

7.1 Recognisor Generator
7.2 Semantic Actions

8. Shen Prolog

8.1 Sample Programs

9. Types

9.1 Types and Constructors
9.2 Functions and Types
9.3 Synonyms

10 Sequent Calculus

10.1 Recursive Types

10.2 Exotic Types

10.2.1 Dependent Types
10.2.2 Negative Types
10.2.3 Subtypes
10.2.4 The Type of All Sets

11 Glossary of Functions

12 The Syntax of Shen

Built by Shen Technology (c) Mark Tarver, September 2021