-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.tex
57 lines (49 loc) · 1.46 KB
/
example.tex
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
\extrapart{Examples}
Here are examples using Pico Scheme.
\begin{scheme}
(define list (lambda l l))
(list 'a 'b 'c) \ev (a b c)
\end{scheme}
\begin{scheme}
(define list? (lambda (l)
(cond ((null? l) \schtrue)
((not (pair? l)) \schfalse)
(else (list? (cdr l))))))
(list? '(a b c)) \ev \schtrue
(list? '(a . b)) \ev \schfalse
\end{scheme}
Returns a list consisting of the elements of {\em l} followed by {\em t}
\begin{scheme}
(define append (lambda (l t)
(cond ((null? l) t)
(else
(cons (car l) (append (cdr l) t))))))
(append '() '(a)) \ev (a)
(append '(a b) '(c d)) \ev (a b c d)
\end{scheme}
This procedure returns the first sublist of {\em l} whose car is {\em obj}.
\begin{scheme}
(define assv (lambda (obj l)
(cond ((null? l) \schfalse)
((eqv? obj (car (car l))) (car l))
(else (assv obj (cdr l))))))
(define e '((a 1) (b 2) (c 3)))
(assv 'a e) \ev (a 1)
(assv 'b e) \ev (b 2)
(assv 'd e) \ev \schfalse
(assv 5 ’((2 3) (5 7) (11 13)))
\ev (5 7)
\end{scheme}
This shows using the Y combinator to create a recursive function.
\begin{scheme}
(let ((Y (lambda (phi)
((lambda (f) (f f))
(lambda (f)
(phi (lambda x (apply (f f) x))))))))
(let ((fact
(Y (lambda (fact)
(lambda (n)
(if (< n 2) 1
(* n (fact (- n 1)))))))))
(fact 5))) \ev 120
\end{scheme}