Professional

Home
Learn Shen
Videos
Community Wiki
Community
OS Kernel
OS Library
Shen Professional

 

The List and Tuples Package

The Standard Extension contains a suite for list and tuples manipulation. The list suite in SP includes several basic utility functions not available in the Shen kernel.

Functions

count : A --> (list A) --> number
Given x, y; returns the number of top level occurrences of x in y.

(count a [a b c d e a])
2 : number


drop : number --> (list A) --> (list A)
Given n, x; drops the first n elements of x. Returns an error if n is out of range.

(drop 3 [a b c d e a])
[d e a] : (list symbol)


drop-last : number --> (list A) --> (list A)
Given n, x; drops the last n elements of x.

(drop-last 3 [a b c d e a])
[a b c] : (list symbol)


every? : (A --> boolean) --> (list A) --> boolean
Given f and x; returns
true just when f is true for every element of x.

(every? (function integer?) [1 2 3])
true : boolean

(every? (function integer?) [1 2 3 5.7])
false : boolean


index : A --> (list A) --> number
Given x, y; returns the position of the first occurrence x in y or
-1 if x does not occur.

(index a [a b c d e a])
1 : number


index-last : A --> (list A) --> number
Given x, y; returns the position of the last occurrence x in y or
-1 if x does not occur.

(index-last a [a b c d e a d f g a])
10 : number


insert : number --> A --> (list A) --> (list A)
Given n, x, y; inserts x into y at a position given by n. Returns an error if n is out of range.

(insert 3 x [a b c d e f])
[a b x c d e f] : (list symbol)


make-list : A --> number --> (list A)
Given n, x; returns a list with x repeated n times.

(make-list a 4)
[a a a a] : (list symbol)


p-assoc : A --> (list (A * B)) --> (A * B)
Given x and y; returns the first tuple in y whose first element is identical with x or raises an error.

(p-assoc c [(@p a 1) (@p b 2) (@p c 3)])
(@p c 3) : (symbol * number)


pop
Given a symbol g bound to a global list l; returns the head of l and binds g to the tail of l. Effectively of type symbol --> A where g is bound to a value of (list A).

(value *n*)
[6 6] : (list number)

(pop *n*)
6 : number

(value *n*)
[6] : (list number)


prefix? : (list A) --> (list A) --> boolean
Given x, y returns
true just when x is a prefix of y.

(prefix? [a b c] [a b c d e f])
true : boolean

(prefix? [a b c e] [a b c d e f])
false : boolean


prefix-length : (list A)--> (list A)--> number
Gives the length of the greatest common prefix of two lists.

(prefix-length [a b c] [a b c d ef])
3 : number

(prefix-length [a b c e] [a b c d ef])
3 : number

(prefix-length [a b e] [a b c d ef])
2 : number

product : (list A) --> (list B)--> (list (A * B))
Gives the Cartesian product of two lists.


push
Given x and g; g is a symbol reserved for binding to an object of type (list A) and x is of type A. If g is bound to l; binds g to the result of consing x to l. If g is unbound, binds g to [x]. Effectively of type
A --> symbol --> (list A) where g is reserved for a value of (list A).

(20+) (datatype globals

___________________
(value *n*) : (list number);)
type#globals : symbol

(21+) (value *n*)
The variable *n* is unbound.

(22+) (push 6 *n*)
[6] : (list number)

(23+) (value *n*)
[6] : (list number)

(24+) (push 6 *n*)
[6 6] : (list number)

(25+) (value *n*)
[6 6] : (list number)


replace : A --> (list A) --> (list A) --> (list A)
Given x, y, z; replaces every occurrence of x in z by y by appending y within z.

(replace a [e f] [a b c a])
[e f b c e f] : (list symbol)


some? : (A --> boolean) --> (list A)--> boolean
Given f and x; returns
true just when f is true for some element of x.

(some? (> 4) [11 42 73 5.7])
false : boolean

(some? (> 6) [11 42 73 5.7])
true : boolean


splice : number --> (list A) --> (list A)--> (list A)
Given n, x, y; appends x within y at position n. Returns an error if n is out of range.

(splice 5 [x y z] [a b c d e f])
[a b c d x y z e f] : (list symbol)


subset? : (list A) --> (list A) --> boolean
Given x, y; returns
true if every element in x occurs in y.

(subset? [1 2 3] [1 2 3 4 5 6])
true : boolean

(subset? [0 1 2 3] [1 2 3 4 5 6])
false : boolean


suffix? : (list A) --> (list A) --> boolean
Given x, y; returns
true just when x is a suffix of y.

(suffix? [e f g] [a b c d e f g])
true : boolean

(suffix? [e f g] [a b c d e f g h])
false : boolean


take : number --> (list A) --> (list A)
Given, n, x; takes the first n elements of x. Returns an error if n is out of range.

(take 3 [a b c d e f g])
[a b c] : (list symbol)


take-last : number --> (list A) --> (list A)
Given, n, x; takes the last n elements of x. Returns an error if n is out of range.

(take-last 3 [a b c d e f g])
[e f g] : (list symbol)