home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 12 / CD_ASCQ_12_0294.iso / maj / 666 / h6.hlp < prev    next >
Text File  |  1992-12-05  |  6KB  |  162 lines

  1.  
  2.                                SEQUENCES
  3.  
  4. [ e1, e2, ..., en]             sequence enumeration
  5. [ e1 .. ek ]                   sequence of integers from e1 to ek
  6. []                             empty sequence
  7. [ e : x <- s ]                 sequence construction
  8. [ e : x <- s ; b ]             sequence construction with boolean
  9. [ e : x <- s1 , y <- s2 ]        s1, s2 are sequences
  10. [ e : x <- s1 , y <- s2 ; b ]    b is a boolean
  11. len s                          length of a sequence
  12. s1 == s2                       sequence equality
  13. s1 conc s2                     concatenation
  14. s[i]                           selection
  15. s[i,r]                         selection with default error return
  16. hd s                           head of a sequence
  17. tl s                           tail of a sequence
  18. butlast s                      first n-1 elements
  19. last s                         [s[len s]]
  20. dom s                          set of indices of s
  21. rng s                          set of elements of s
  22. seq2map s                      convert sequence to a map
  23. map2seq m                      convert map to a sequence
  24.  
  25. A sequence is a collection of ordered elements which may be
  26. selected by their ordinal position (index) in the sequence. The
  27. types of the elements are not necessarily the same.
  28.  
  29. A sequence can be enumerated by delimiting its elements by square
  30. brackets:
  31.  
  32. [1,2,3,4,5]
  33.  
  34. This same sequence can be constructed by using the range (not to be
  35. confused with the range of a map) notation:
  36.  
  37. [1..5]
  38.  
  39. Ranges can only be used when the elements are consecutive. Another
  40. example of enumeration would be
  41.  
  42. s1 = [1,2,2,3,4];
  43.  
  44. Note the duplicate elements. To find the number of elements in the
  45. sequence s1, we use the length operator len:
  46.  
  47. len s1;                   returns 5
  48.  
  49. Two sequences can be tested for equality using the equality operator
  50. ==. Concatenation of two sequences is performed used the concatenation
  51. operator conc.
  52.  
  53. [1,2,3,4] conc [3,4,5];   returns [1,2,3,4,3,4,5]
  54.  
  55. If s=[3,4,5], then an element of the sequence can be selected using
  56. its index in the sequence. For example,
  57.  
  58. s[1];                     returns 3
  59. s[3];                     returns 5
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.                                      Page 9
  70.  
  71.  
  72. s[4];                     returns false
  73. s[4,0];                   returns 0
  74.  
  75. The last example uses the default error return.
  76.  
  77. Other selection operators on sequences are hd, tl, last and butlast.
  78. Hd returns the first element of the sequence. Tl returns a sequence
  79. consisting of every element but the first. Last returns a sequence
  80. consisiting of only the last element. Butlast returns a sequence
  81. consisting of every element but the last.
  82.  
  83. hd s;                     returns 3
  84. tl s;                     returns [4,5]
  85. last s;                   returns [5]
  86. butlast s;                returns [3,4]
  87.  
  88. A sequence of length n can be represented by a map whose range elements
  89. are the elements of the sequence, and whose domain elements are the
  90. integers from 1 to n. Consequently, the domain operator applied to a
  91. sequence will return the index elements {1..n}, and the range operator
  92. will return the elements of the sequence contained in a set.
  93.  
  94. dom s;                    returns {1,2,3}
  95. rng s;                    returns {3,4,5}
  96.  
  97. A sequence can be converted explicitly to its corresponding map and
  98. conversely. Thus
  99.  
  100. m=seq2map s;              assigns to m the map {1->3,2->4,3->5}
  101. map2seq m;                returns [3,4,5]
  102.  
  103. A more general way of constructing sequences, analogous to set
  104. construction is given by the following form
  105.  
  106. [ f(x): x<- expr ; pred(x) ]
  107.  
  108. where the syntax x<- expr is called a generator, and the syntax
  109. ; pred(x) is optional. The expression expr is evaluated to
  110. yield a sequence. The values of the sequence are successively assigned
  111. to x and a new sequence is formed from elements obtained by applying the
  112. function f to the successive values of x which satisfy pred(x) (if pred(x)
  113. occurs). An example:
  114.  
  115. [len x: x<-["abc","defg","hijkl"];len x > 3];  returns [4,5]
  116.  
  117. Before concluding a discussion of sequences, it is important to point
  118. out a distinction between a sequence of length two and an orderd pair.
  119. Remember that a map may be considered to be a set of ordered pairs.
  120. There are cases when one needs to access the components of an ordered
  121. pair. Proxy provides two operators for this purpose, first and second.
  122.  
  123. {first x:x<-{1->2,3->4,5->6}};     returns {1,3,5}
  124.  
  125. Suppose now that we have a set of sequences, say y={[1,2],[3,4]}
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.                                      Page 10
  136.  
  137.  
  138. and we wish to obtain a set of the first elements of the sequences.
  139. It would be wrong to write {first x:x<-y}. The correct solution is to
  140. write {x[1]:x<-y}.
  141.  
  142. Strings
  143.  
  144. Strings can be considered to be sequences of characters, although
  145. a character is not a primitive type in Proxy. Since the string
  146. operators are similar to the sequence operators already discussed,
  147. their use will be shown below in examples.
  148.  
  149. len "abcd";                      returns 4
  150. "abc" == "abc";                  returns true
  151. "abc" == "aBc";                  returns false
  152. "abc" < "abcd";                  returns true
  153. "abc" conc "defg";               returns "abcdefg"
  154. s="abc";
  155. s[2];                            returns "b"
  156. s[4];                            returns false
  157. s[4,0];                          returns 0
  158. hd "abc";                        "a"
  159. tl "abc";                        "bc"
  160. last "abc";                      "c"
  161. butlast "abc";                   "ab"
  162.