Perfect Developer basic tutorial 3 | This page last modified 2011-10-29 (JAC) |
You can construct a seq of char using a quoted string.
For example,
"hello"
means the same as
seq of char{`h`,`e`,`l`,`l,`o`}
.
Elements in a seq can be accessed by position. Following convention in
most programming languages, the indexing selector is represented by square brackets.
Valid indices range from 0 to one less than the length of the sequence; for
example, "abc"[1]
yields
`b`
, while
"abc"[3]
is invalid.
The index of the last element of a sequence
x
is readily expressed as
<#x
which reads
"predecessor-of length-of x".
Quite often, it is useful to process a sequence by splitting it into a single
element and the remainder. To split after the first element, use the member
head to get the first element and tail to get the rest. If you wish
to split just before the last element, use members front and last
instead. Naturally, x.head
is equivalent to x[0]
,
and x.last
is the same as
x[<#x]
.
Aside from using constructors and string literals, there is another way to
construct a sequence of elements where the element type is char, int
or an enumeration class. All such classes define the ..
binary operator,
which constructs a sequence of ordered values from the first to the second
operand inclusive. For example,
1..4
is equivalent to
seq of int{1,2,3,4}
. If the second operand is less than the first, an
empty sequence is returned.
Other useful methods include:
See the Class Library Reference in the Language Reference Manual for other methods of class seq of X.
take(n) returns the first n elements of the sequence (n must not exceed the length of the sequence) drop(n) returns all except the first n elements of the sequence (n must not exceed the length of the sequence) slice(d, t) returns the first t elements starting at position d (d+t must not exceed the length of the sequence) begins(s) returns true if the sequence starts with the sequence s ends(s) returns true if the sequence ends with the sequence s isndec returns true if the sequence is non-decrementing (i.e. each element is not less than its predecessor) isninc returns true if the sequence is non-incrementing (i.e. each element is not greater than its predecessor)
Knowledge round-up quiz
Next: Declaring Subtypes
Save My Place | Glossary | Language Reference Manual |
Copyright © 1997-2012 Escher Technologies Limited. All rights reserved. Information is subject to change without notice. |