Diff of /Test/examples/learn_pyke.tst [000000] .. [4dca5a]  Maximize  Restore

Switch to unified view

a b/Test/examples/learn_pyke.tst
1
# learn_pyke.tst
2
3
    >>> import sys
4
    >>> import pyke
5
    >>> import os
6
    >>> new_path = os.path.join(os.path.dirname(os.path.dirname(pyke.__file__)),
7
    ...                         'examples/learn_pyke')
8
    >>> sys.path.append(new_path)
9
10
    >>> import run
11
    >>> from io import StringIO
12
    >>> import sys
13
14
    >>> sys.stdin = StringIO('8\n2\n2\n13\n')
15
    >>> run.run()
16
    ______________________________________________________________________________
17
    Assume that the following two patterns are contained in different rules
18
    and that none of the pattern variables are initially bound to values:
19
    <BLANKLINE>
20
    pattern 1: ((ho, $_, ($a, $a)), ($a, $a, $b), ($a, *$b))
21
    pattern 2: ($a, $a, $x)
22
    <BLANKLINE>
23
    If the two patterns are matched together, what will $x be bound to?
24
      1. (a, b)
25
      2. $a
26
      3. ho
27
      4. ($a, *$b)
28
      5. (ho, *$b)
29
      6. (ho, *($a, $a))
30
      7. (ho, ($a, $a))
31
      8. (ho, $a, $a)
32
      9. (ho, *(ho, ho))
33
     10. (ho, (ho, ho))
34
     11. (ho, $_, (ho, ho))
35
     12. (ho, ho, (ho, ho))
36
     13. (ho, ho, ho)
37
     14. nothing, the two patterns don't match
38
     15. nothing, pattern 1 is not a legal pattern
39
     16. I don't have a clue...
40
    ? [1-16] Incorrect: Pattern variable '$a' is bound to a value.
41
    ______________________________________________________________________________
42
    "Rest" pattern variables are used at the end of a tuple pattern to match the
43
    rest of the tuple.
44
    <BLANKLINE>
45
    What is the syntax for a "rest" pattern variable?
46
      1. $rest
47
      2. _rest
48
      3. Preceding a pattern variable with an asterisk ('*'), like: *$foo.
49
    ? [1-3] Incorrect: A "rest" pattern variable is any pattern variable preceded
50
               by an asterisk ('*').
51
    ______________________________________________________________________________
52
    After matching the following two patterns, what is $c set to?
53
    <BLANKLINE>
54
    pattern 1: ($a, $b, *$c)
55
    pattern 2: (1, 2, 3)
56
      1. 3
57
      2. (3)
58
      3. (3,)
59
      4. nothing, the two patterns don't match
60
      5. nothing, pattern 1 is not a legal pattern
61
    ? [1-5] Correct!  (Note that a comma is not required for singleton tuples in PyKE).
62
    ______________________________________________________________________________
63
    Assume that the following two patterns are contained in different rules
64
    and that none of the pattern variables are initially bound to values:
65
    <BLANKLINE>
66
    pattern 1: ((ho, $_, ($a, $a)), ($a, $a, $b), ($a, *$b))
67
    pattern 2: ($a, $a, $x)
68
    <BLANKLINE>
69
    If the two patterns are matched together, what will $x be bound to?
70
      1. (a, b)
71
      2. $a
72
      3. ho
73
      4. ($a, *$b)
74
      5. (ho, *$b)
75
      6. (ho, *($a, $a))
76
      7. (ho, ($a, $a))
77
      8. (ho, $a, $a)
78
      9. (ho, *(ho, ho))
79
     10. (ho, (ho, ho))
80
     11. (ho, $_, (ho, ho))
81
     12. (ho, ho, (ho, ho))
82
     13. (ho, ho, ho)
83
     14. nothing, the two patterns don't match
84
     15. nothing, pattern 1 is not a legal pattern
85
     16. I don't have a clue...
86
    ? [1-16] Correct!
87
        matching Pattern 1: (ho, $_, ($a, $a))
88
              to Pattern 2: $a
89
           binds Pattern 2: $a to Pattern 1: (ho, $_, (ho, ho))
90
        matching Pattern 1: ($a, $a, $b)
91
              to Pattern 2: $a, which is bound to Pattern 1: (ho, $_, ($a, $a))
92
           binds Pattern 1: $a to ho,
93
             and Pattern 1: $b to Pattern 1: ($a, $a) which expands to (ho, ho)
94
        matching Pattern 1: ($a, *$b)
95
              to Pattern 2: $x
96
           binds Pattern 2: $x to Pattern 1: ($a, *$b) which expands to (ho, ho, ho)
97