home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
HomeWare 14
/
HOMEWARE14.bin
/
prog
/
proxy14.arj
/
H6.HLP
< prev
next >
Wrap
Text File
|
1992-12-05
|
6KB
|
162 lines
SEQUENCES
[ e1, e2, ..., en] sequence enumeration
[ e1 .. ek ] sequence of integers from e1 to ek
[] empty sequence
[ e : x <- s ] sequence construction
[ e : x <- s ; b ] sequence construction with boolean
[ e : x <- s1 , y <- s2 ] s1, s2 are sequences
[ e : x <- s1 , y <- s2 ; b ] b is a boolean
len s length of a sequence
s1 == s2 sequence equality
s1 conc s2 concatenation
s[i] selection
s[i,r] selection with default error return
hd s head of a sequence
tl s tail of a sequence
butlast s first n-1 elements
last s [s[len s]]
dom s set of indices of s
rng s set of elements of s
seq2map s convert sequence to a map
map2seq m convert map to a sequence
A sequence is a collection of ordered elements which may be
selected by their ordinal position (index) in the sequence. The
types of the elements are not necessarily the same.
A sequence can be enumerated by delimiting its elements by square
brackets:
[1,2,3,4,5]
This same sequence can be constructed by using the range (not to be
confused with the range of a map) notation:
[1..5]
Ranges can only be used when the elements are consecutive. Another
example of enumeration would be
s1 = [1,2,2,3,4];
Note the duplicate elements. To find the number of elements in the
sequence s1, we use the length operator len:
len s1; returns 5
Two sequences can be tested for equality using the equality operator
==. Concatenation of two sequences is performed used the concatenation
operator conc.
[1,2,3,4] conc [3,4,5]; returns [1,2,3,4,3,4,5]
If s=[3,4,5], then an element of the sequence can be selected using
its index in the sequence. For example,
s[1]; returns 3
s[3]; returns 5
Page 9
s[4]; returns false
s[4,0]; returns 0
The last example uses the default error return.
Other selection operators on sequences are hd, tl, last and butlast.
Hd returns the first element of the sequence. Tl returns a sequence
consisting of every element but the first. Last returns a sequence
consisiting of only the last element. Butlast returns a sequence
consisting of every element but the last.
hd s; returns 3
tl s; returns [4,5]
last s; returns [5]
butlast s; returns [3,4]
A sequence of length n can be represented by a map whose range elements
are the elements of the sequence, and whose domain elements are the
integers from 1 to n. Consequently, the domain operator applied to a
sequence will return the index elements {1..n}, and the range operator
will return the elements of the sequence contained in a set.
dom s; returns {1,2,3}
rng s; returns {3,4,5}
A sequence can be converted explicitly to its corresponding map and
conversely. Thus
m=seq2map s; assigns to m the map {1->3,2->4,3->5}
map2seq m; returns [3,4,5]
A more general way of constructing sequences, analogous to set
construction is given by the following form
[ f(x): x<- expr ; pred(x) ]
where the syntax x<- expr is called a generator, and the syntax
; pred(x) is optional. The expression expr is evaluated to
yield a sequence. The values of the sequence are successively assigned
to x and a new sequence is formed from elements obtained by applying the
function f to the successive values of x which satisfy pred(x) (if pred(x)
occurs). An example:
[len x: x<-["abc","defg","hijkl"];len x > 3]; returns [4,5]
Before concluding a discussion of sequences, it is important to point
out a distinction between a sequence of length two and an orderd pair.
Remember that a map may be considered to be a set of ordered pairs.
There are cases when one needs to access the components of an ordered
pair. Proxy provides two operators for this purpose, first and second.
{first x:x<-{1->2,3->4,5->6}}; returns {1,3,5}
Suppose now that we have a set of sequences, say y={[1,2],[3,4]}
Page 10
and we wish to obtain a set of the first elements of the sequences.
It would be wrong to write {first x:x<-y}. The correct solution is to
write {x[1]:x<-y}.
Strings
Strings can be considered to be sequences of characters, although
a character is not a primitive type in Proxy. Since the string
operators are similar to the sequence operators already discussed,
their use will be shown below in examples.
len "abcd"; returns 4
"abc" == "abc"; returns true
"abc" == "aBc"; returns false
"abc" < "abcd"; returns true
"abc" conc "defg"; returns "abcdefg"
s="abc";
s[2]; returns "b"
s[4]; returns false
s[4,0]; returns 0
hd "abc"; "a"
tl "abc"; "bc"
last "abc"; "c"
butlast "abc"; "ab"