6.3.1.1. Generative Expressions
Working with expressions without the benefit of procedures is a good way to learn what generators are all about. The following sections provide some examples and pose some problems that may be helpful in learning to think in Icon.
Problems: Analyzing Expressions
It generally is easier to figure out what an expression does than to craft one to produce a desired result. Here are some expressions for generating sequences of values. Try to determine their sequences. For example,
(1 to 10 by 2) \ 3
has the sequence 1, 3, 5.
- 1. (1 to 3) | (3 to 1 by 1)
Solution: 1, 2, 3, 3, 2, 1. The sequence for alternation consists of the sequence for its first operand followed by the sequence for its second operand.
- 2. (1 | 2) to 3
Solution: 1, 2, 3, 2, 3. The expression (1 | 2) to 3 is equivalent to (1 to 3) | (2 to 3).
- 3. 1 to (2 | 3)
Solution: 1, 2, 1, 2, 3. The expression 1 to (2 | 3) is equivalent to (1 to 2) | (1 to 3).
- 4. (1 to 3) | (3 to 1 by 2)
Solution: 1, 2, 3, 3, 1.
- 5. 3 to (1 | 2)
Solution: Nothing. If the first operand of to-by is greater than the second and the increment is positive (1 by default here), no value is produced.
- 6. 1 & 2
Solution: 2. Conjunction generates the sequence for its second operand, which is just 2 in this case.
- 7. (1 to 3) & 2
Solution: 2, 2, 2. In the expression, (1 to 3) & 2, (1 to 3) generates three results; for each one the conjunction is evaluated, producing its second operand, 2. This expression is equivalent to (1 & 2) | (2 & 2) | (3 & 2).
- 8. (1 | 2) & 2
Solution: 2, 2.
- 9. 1 & (1 to 3)
Solution: 1, 2, 3. See the comment in the solution to problem 6.
- 10. (3 to 5) & (1 to 3)
Solution: 1, 2, 3, 1, 2, 3, 1, 2, 3. Because the first operand of the conjunction, (3 to 5), produces three results, and the second argument, (1 to 3), is evaluated three times. The values produced by the first operand of conjunction are irrelevant.
- 11. |(1 to 3)
Solution: 1, 2, 3, 1, 2, 3, 1, 2, 3,
. Repeated alternation produces the sequence for its operand repeatedly. In this case, it is equivalent to (1 to 3) | (1 to 3) | (1 to 3)
. Although the sequence is infinite, the number of results actually produced depends on the context in which it is evaluated.
- 12. |((1 to 3) | (3 to 1 by 1))
Solution: 1, 2, 3, 3, 2, 1, 1, 2, 3, 3, 2, 1, 1, 2, 3, 3, 2, 1,
. See the comments on the solution to problem 1.
- 13. |3
Solution: 3, 3, 3,
.
- 14. |((1 to 10 by 2) \ 3)
Solution: 1, 3, 5, 1, 3, 5, 1, 3, 5, 1, 3, 5, 1, 3, 5, 1, 3, 5, 1, 3,
. The expression (1 to 10 by 2) generates 1, 3, 5, 7, 9, but the limitation to three values truncates this to 1, 3, 5.
- 15. |(3 to (1 | 2))
Solution: Nothing. Because the expression (3 to (1 | 2)) produces no value as mentioned in the solution to problem 5, repeated alternation produces no value.
- 16. |(1 & 2)
Solution: 2, 2, 2,
. See the solution to problem 6.
- 17. |(1 \ 2)
Solution: 1, 1, 1,
. Limiting the expression 1 to two values has no effect because 1 produces only one value.
- 18. |((3 to 5) & (1 | 2))
Solution: 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2,
. See the solution to problem 10.
- 19. (|((1 to 3) & (1 | 2))) \ 5
Solution: 1, 2, 1, 2, 1. The operand of repeated alternation produces 1, 2, 1, 2, 1, 2, and repeated alternation would produce this repeatedly except for the limitation to five values, which occurs before any repetition.
- 20. !&lcase
Solution: a, b, c,
, x, y, z. Because &lcase is a cset and the element generation operation applies only to strings, &lcase is converted automatically to a string of the 26 lowercase letters. Note that the values are strings, not csets.
- 21. |!&lcase
Solution: a, b, c,
, x, y, z, a, b, c,
, x, y, z, a, b, c,
, x, y, z
.
- 22. !reverse(&lcase)
Solution: z, y, x,
, c, b, a.
- 23. reverse(!&lcase)
Solution: a, b, c,
, x, y, z, a, b, c,
, x, y, z, a, b, c,
, x, y, z
. The function reverse() is applied to the sequence for !&lcase as given in the solution to problem 20. Reversing a one-character string has no effect.
- 24. |?HT
Solution: H, H, H, T, H, H,
. The sequence for ?HT is produced repeatedly. The values are H and T with approximately equal probability. The actual sequence produced varies from evaluation to evaluation as the pseudo-random sequence advances.
- 25. ?|HT
Solution: T, T, H, T, H, T,
. The expressions |?HT and ?|HT are essentially the same. The first corresponds to ?HT | ?HT | ?HT |
, and the second corresponds to ?(HT | HT | HT
).
- 26. !100
Solution: 1, 0, 0. This one is subtle. As mentioned in the solution to problem 20, the element generation operation applies only to strings and structures. The integer argument 100 therefore is converted automatically to the string 100.
- 27. !(1 to 3)
Solution: 1, 2, 3. The expression (1 to 3) produces the integers 1, 2, and 3. Each is converted to a string as described in the solution to the preceding problem. Element generation applied to a one-character string just produces the string.
- 28. (1 to !3)
Solution: 1, 2, 3. The expression !3 produces 3, which is converted to an integer automatically.
- 29. (1 to |3)
Solution: 1, 2, 3, 1, 2, 3, 1, 2, 3,
. The expression |3 produces 3, 3, 3,
, so the repeated alternation is equivalent to (1 to 3) | (1 to 3) | (1 to 3)
. Note that (1 to |3) is equivalent to |(1 to 3).
- 30. !12 to 3
Solution: 1, 2, 3, 2, 3. The expression !12 has the sequence 1, 2. Therefore !12 to 3 is equivalent to (1 | 2) to 3, which is equivalent to ((1 | 2) to 3), which is equivalent to (1 to 3) | (2 to 3).
|