ShenDoc 30


 

 

Strings and Pattern Matching

The polyadic function @s can be used to concatenate n (n >= 2) strings. The polyadicity is a syntactic fiction maintained by the Shen reader. (@s "a" "b" "c") is parsed as (@s "a" (@s "b" "c")) exactly in the manner of @p.

The Shen reader parses (@s "123" "456") in a special way; as (@s "1" (@s "2" (@s "3" "456"))). The leading argument, if a string, is decomposed into a series of concatenations of unit strings. The significance of this is realised in the use of @s for pattern-matching over strings.

@s is not a fast operation because many platforms represent strings as vectors and in these cases @s runs in linear time in respect of the size of the arguments.

Within a function @s may be used for pattern-matching. For example; the following removes all occurences of my Christian name from a string.

(define remove-my-name
  {string --> string}
  "" -> ""
  (@s "Mark" Str) -> (remove-my-name Str)
  (@s S Str) -> (@s S (remove-my-name Str)))

which is parsed into the following.

(define remove-my-name
  {string --> string}
  "" -> ""
  (@s "M" (@s "a" (@s "r" (@s "k" Str)))) -> (remove-my-name Str)
  (@s S Str) -> (@s S (remove-my-name Str)))
Acknowledgements

History
Basic Types in Shen and Kλ
The Primitive Functions of Kλ
The Syntax of Kλ
Notes on the Implementation of Kλ
Boolean Operators
The Syntax of Symbols
The Semantics of Symbols in Shen and Kλ
Packages
Prolog
Shen-YACC
Strings
Strings and Pattern Matching
Lists
Streams
Character Streams and Byte Streams
Bytes and Unicode
Reader Macros
Vectors
Standard Vectors and Pattern Matching
Non-standard Vectors and Tuples
Equality
I/O
Generic Functions
Eval
Type Declarations
External Global Variables
Property Lists and Hashing
Error Handling
Numbers
Floats and Integers
The Timer
Comments
Special Forms

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