vile / scheme / testdata / frontends / loops.vile
29 lines · 29 sloc · 663 bytes · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1import((vile,base))
2define-syntax(skip-even,syntax-rules((),((_,discarded),continue())))
3sum-odds(n) := {
4 total := 0
5 for(i := 0, i < n, i = i + 1) {
6 if even?(i) { skip-even(missing) }
7 total = total + i
8 }
9 total
10}
11find-negative(xs) := {
12 hit := #f
13 for(i := 0, i < vector-length(xs), i = i + 1) {
14 if xs[i] < 0 { hit = i, break }
15 }
16 return(hit)
17 missing
18}
19capture-loop-locals() := {
20 fs := list()
21 for(i := 0, i < 3, i = i + 1) {
22 x := i
23 fs = cons(fn(){x},fs)
24 continue
25 }
26 map(fn(f){f()},fs)
27}
28list(sum-odds(10),find-negative([1,2,-3,4]),capture-loop-locals(),
29 let((continue(+),break(*)),list(continue(20,22),break(6,7))))
30