






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







1
2
3
( λ ( x ) _xxx_** ) (2) yields 8
4
5
s-expression ::= literals | symbols | ( s-expression... )
6
#t, #f
#\a, #\newline, "Hello World!"
f, x, +, *, foo, bar, null?, set!
10
(quote x), ’x
’() , often assigned to the variable nil
’(1 2 3), ’(a b 3), ’(1 (2 3) 4)
11
cons /
1 cons /
2 ()
12
The list ’(1 (2 3) 4) is stored as cons /
1 cons /
cons cons / \ /
2 cons 4 () /
3 ()
13
car, cdr, cons
(car ’(1 2)) returns 1 (cdr ’(1 2)) returns (2) (cons 1 ’(2)) returns (1 2) (cons 1 2) returns (1. 2)
14
Not a list (1. 2)
15
(car (cdr ’(1 2 3))) 2 (cadr ’(1 2 3))) 2 (cons 1 ’()) (1) (cons 1 2) (1. 2) (cons ’() ’()) (()) (car ’(())) ()
19
;; Curried version of twice (define (twice f) (lambda (x) (f (f x))))
(define quadruple (twice double)) (quadruple 2) ; returns 8 ((twice double) 2) ; returns 8 ((twice quadruple) 2) ; returns 32 ((twice (twice double)) 2) ; returns 32
20
(int ()(int)) twice (int (f)(int)) { int foo (int x) { return f(f(x)); }
return foo; }
(twice(double))(2);
21
(if (= x y) 1 2) ; in C: (x == y)? 1 : 2
(cond ((= x y) 1) ((< x y) 2) (else 3))
(if 1 2 3) ; returns 2
22
(eq? x x) #t (eq? ’x ’x) #t (eq? ’() ’()) #t (eq? 2 2) undefined (eq? (list 1) ’(1)) #f
(eqv? 2 2) #t (equal? (list 1) ’(1)) #t
23
(define (fac n) (if (= n 0) 1 (* n (fac (- n 1)))))
(fac 5) ; returns 120 (fac 1000) ; that’s 2568 digits
24
(define (mymap f l) (if (null? l) ’() (cons (f (car l)) (mymap f (cdr l)))))
(mymap double ’(1 2 3 4 5)) ; returns (2 4 6 8 10) (mymap (lambda (x) (+ x 1)) ’(1 2)) ; returns (2 3)
28
(define (gt p ls) (filter (lambda (x) (> p x)) ls))
(define (le p ls) (filter (lambda (x) (<= p x)) ls))
29
(define (filter pred ls) (cond ((null? ls) ’()) ((pred (car ls)) (cons (car ls) (filter pred (cdr ls)))) (else (filter pred (cdr ls)))))
30
(define x 3) 3 (set! x 5) 5
(define x (list 1 2)) (1 2) (set-car! x 3) (3 2) (set-cdr! x 4) (3. 4)