Skip to content

Commit 21362aa

Browse files
committed
Add example for search
1 parent 33c3247 commit 21362aa

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
import stream
3+
import search
4+
5+
type Tree[A] {
6+
Nil()
7+
Branch(v: A, l: Tree[A], r: Tree[A])
8+
}
9+
10+
def notEach(t: Tree[Bool]): Tree[Bool] / {fork, fail} = t match {
11+
case Nil() =>
12+
do fail()
13+
case Branch(v, l, r) =>
14+
choice {
15+
Branch(not(v), l, r)
16+
} {
17+
choice {
18+
Branch(v, notEach(l), r)
19+
} {
20+
Branch(v, l, notEach(r))
21+
}
22+
}
23+
}
24+
25+
def count[A] { stream: => Unit / emit[A] }: Int = {
26+
var n = 0
27+
for[A] { stream() } { _ => n = n + 1 }
28+
n
29+
}
30+
31+
def main() = {
32+
val example = Branch(false, Branch(false, Nil(), Branch(false, Nil(), Nil())), Nil())
33+
println(count[Tree[Bool]] { results { notEach(example) } })
34+
}
35+

0 commit comments

Comments
 (0)