-
Notifications
You must be signed in to change notification settings - Fork 5
/
input.tcl
131 lines (108 loc) · 2.51 KB
/
input.tcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Set a variable
set a 43.1
puts "Variable a, ($$a), is set to: $a"
// variable expansion comes before execution.
set a pu
set b ts
$a$b "Hello World"
// expansion, once again.
// replacing things between the brackets with the output from executing them
puts [set a 4]
puts [set a]
// Variables can be longer.
set name "Steve Kemp"
puts "Hello World my name is $name"
// We have a standard library, located in `stdlib/stdlib.tcl`
//
// The standard library contains a couple of helpful methods,
// one of which is `assert_equal`.
//
// This will do "string" or "number" comparisons, and terminate
// execution on failure.
//
assert_equal "$name" "Steve Kemp"
assert_equal 9 [expr 3 * 3]
assert_equal 7.4 [- [+ 7 1.4] 1]
assert_equal 12 [+ 10 2]
// conditional
if { 1 } { puts "OK: 1 was ok" }
if { 0 } { puts "FAILURE: 0 was regarded as true" }
if { "steve" } { puts "OK: steve was ok" } else { puts "steve was not ok" }
// More conditionals
// remember we set some variables earlier:
//
// "a" => "pu"
// "b" => "ts"
// "x" => UNDEFINED
//
if { $a } { puts "$$a is set" } else { puts "$$a is NOT set" }
if { $x } { puts "$$x is set - This is a bug" } else { puts "$$x is NOT set" }
//
// Setup some variables for a loop.
//
set i 1
set max 10
set sum 0
//
// Now we'll run a while-loop to sum some numbers
//
while { expr $i <= $max } {
puts " Loop $i"
incr sum $i
incr i
}
// Show the sum
puts "Sum of 1..10 (==(10x11)/2): $sum"
//
// Our first user-defined function!
//
proc inc {x} { puts "$$x is $x"; expr $x + 1 }
puts "3 inc is [inc 3]"
//
// Naive/Recursive solution.
//
proc fib {x} {
if { expr $x <= 1 } {
return 1
} else {
return [expr [fib [expr $x - 1]] + [fib [expr $x - 2]]]
}
}
//
// A better, non-recursive, solution.
//
proc fib2 {n} {
set a 1
set b 1
for {set N $n} {expr [set N] > 0} {decr N} {
set tmp [+ $a $b]
set a $b
set b $tmp
}
return $a
}
//
// Lets run this in a loop
//
set i 0
set max 15
while { expr $i <= $max } {
puts "Fib from a while-loop, with recursion, result $i is [fib $i]"
incr i
}
//
// We can do the same thing again, using a for-loop, and a different (faster)
// Fibonacci sequence generator.
//
for {set i 0} {< $i 50} {incr i} {
puts "Fib from a for-loop, without recursion, result $i is [fib2 $i]"
}
//
// This is just a horrid approach for running eval
//
set a { set b 20 ; incr b ; incr b; puts "$$b is $b" }
eval "$a"
//
// Is this better?
//
eval { set b 20 ; incr b ; incr b; puts "$$b is $b" }