Christian Collberg
Department of Computer Science
University of Arizona
for n := tree_nodes_in_inorder(T) do print n end
LinkedList<String> L = new LinkedList<String>();
L.add("Bebe");
L.add("Wendy");
L.add("Nelly");
Iterator<String> i = L.iterator();
while (i.hasNext()) {
String c = i.next();
System.out.println(c);
}
LinkedList<String> L = new LinkedList<String>();
L.add("Bebe");
L.add("Wendy");
L.add("Nelly");
for (String c : L)
System.out.println(c);
29: aload_1 30: invokevirtual #8; // LinkedList.iterator: 33: astore_2 34: aload_2 35: invokeinterface #9,1; // java/util/Iterator.hasNext 40: ifeq 63 43: aload_2 44: invokeinterface #10,1; // java/util/Iterator.next 49: checkcast #11; // java/lang/String 52: astore_3 53: getstatic #12; // java/lang/System.out 56: aload_3 57: invokevirtual #13; // java/io/PrintStream.println 60: goto 34
$flock = ["huey","dewey","louie"]
def isDuck?(name)
for i in 0...$flock.length
if $flock[i] == name then
return true
end
end
return false
end
puts isDuck?("dewey"), isDuck?("donald")
def isDuck?(name)
$flock.find do |x|
x == name
end
end
puts isDuck?("dewey")
puts isDuck?("donald")
def triplets()
yield "huey"
yield "dewey"
yield "louie"
end
triplets() {|d| puts d}
triplets() do |d|
puts d
end
def fac(n)
f = 1
for i in 1..n
f *= i
yield f
end
end
fac(5) {|f| puts f}
def fac(n)
f = 1
for i in 1..n
f *= i
yield i,f
end
end
fac(5) do |i,x|
puts "#{i}! = #{x}"
end
fac(3) do |i,x|
fac(3) do |j,y|
puts "#{i}! * #{j}! = #{x*y}"
end
end
def sumfac(n)
y = 0
fac(n) do |i,x|
y = y + x
end
return y
end
puts sumfac(5)
def find(arr)
for i in 0..arr.length
if yield arr[i] then return true end
end
return false
end
puts find($flock) {|x| x=="dewey"}
puts find($flock) {|x| x=="donald"}
$flock = ["huey","dewey","louie"]
$flock.each {|x| puts x}
puts $flock.collect {|x| x.length}
puts $flock.collect do |x|
"junior woodchuck, General " + x
end
x = $flock.inject("") do |elmt,total|
total = elmt + " " + total
end
puts x
x = $flock.inject() do |elmt,total|
total = elmt + " " + total
end
puts x
Procedures are really generators; they can return 0, 1, or a sequence of results. There are three cases
procedure To(i,j)
while i <= j do {
suspend i
i+:= 1
}
end
procedure To(i,j)
while i <= j do {
suspend i
i+:= 1
}
end
procedure main()
every k := To(1,3) do
write(k)
end
procedure P()
suspend 3
suspend 4
suspend 5
end
procedure main()
every k := P() do
write(k)
end
> setenv TRACE 100
> simple
: main()
simple.icn : 8 | P()
simple.icn : 2 | P suspended 3
3
simple.icn : 9 | P resumed
simple.icn : 3 | P suspended 4
4
simple.icn : 9 | P resumed
simple.icn : 4 | P suspended 5
5
simple.icn : 9 | P resumed
simple.icn : 5 | P failed
simple.icn : 10 main failed
for i in from_to_by(first,last,step) do ... end
from_to_by = iter(from,to,by:int) yields(int)
i : int := from
if by> 0 then
while i <= to do
yield i
i +:= by
end
else
while i >= to do
yield i
i +:= by
end
end
for t: bin_tree in bin_tree$tree_gen(n) do bin_tree$print(t) end
bin_tree = cluster ...
node = record [left,right : bin_tree]
rep = variant [some : node, empty : null]
...
tree_gen = iter (k : int) yields (cvt)
if k=0 then
yield red$make_empty(nil)
else
for i:int in from_to(1,k) do
for l : bin_tree in tree_gen(i-1) do
for r : bin_tree in tree_gen(k-i) do
yield rep$make_some(node${l,r})
end
end
end
end tree_gen
...
end