-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathopt.scm
37 lines (33 loc) · 1.01 KB
/
opt.scm
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
#!/usr/bin/env ol
; this test showing optimization of the lisp code
(define append1
(letrec ((app (lambda (a b)
(if (null? a)
b
(cons (car a) (app (cdr a) b)))))
(appl (lambda (l)
(if (null? (cdr l))
(car l)
(app (car l) (appl (cdr l)))))))
(case-lambda
((a b) (app a b))
((a b . cs) (app a (app b (appl cs))))
((a) a)
(() '()))))
(define append2
(let*((app (lambda (a b app)
(if (null? a)
b
(cons (car a) (app (cdr a) b app)))))
(appl (lambda (l appl)
(if (null? (cdr l))
(car l) ; don't recurse down the list just to append nothing
(app (car l) (appl (cdr l) appl) app)))))
(case-lambda
((a b) (app a b app))
((a b . cs) (app a (app b (appl cs appl) app) app))
((a) a)
(() '()))))
(assert (equal?
(fasl-encode append1)
(fasl-encode append2)))