-
Notifications
You must be signed in to change notification settings - Fork 0
/
questions.scm
executable file
·72 lines (62 loc) · 1.85 KB
/
questions.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
(define (caar x) (car (car x)))
(define (cadr x) (car (cdr x)))
(define (cdar x) (cdr (car x)))
(define (cddr x) (cdr (cdr x)))
;; Problem 17
;; Returns a list of two-element lists
(define (enumerate s)
; BEGIN PROBLEM 17
(define (enumerate-helper s t)
(if (null? s)
s
(cons (list t (car s)) (enumerate-helper (cdr s) (+ t 1)))))
(enumerate-helper s 0)
)
; END PROBLEM 17
;; Problem 18
(define (zip pairs)
; BEGIN PROBLEM 18
(list (map car pairs) (map cadr pairs))
)
; END PROBLEM 18
;; Problem 19
;; Returns a function that checks if an expression is the special form FORM
(define (check-special form)
(lambda (expr) (equal? form (car expr))))
(define lambda? (check-special 'lambda))
(define define? (check-special 'define))
(define quoted? (check-special 'quote))
(define let? (check-special 'let))
;; Converts all let special forms in EXPR into equivalent forms using lambda
(define (let-to-lambda expr)
(cond ((atom? expr)
; BEGIN PROBLEM 19
expr
; END PROBLEM 19
)
((quoted? expr)
; BEGIN PROBLEM 19
expr
; END PROBLEM 19
)
((or (lambda? expr)
(define? expr))
(let ((form (car expr))
(params (cadr expr))
(body (cddr expr)))
; BEGIN PROBLEM 19
(cons form (cons params (map let-to-lambda body)))
; END PROBLEM 19
))
((let? expr)
(let ((values (cadr expr))
(body (cddr expr)))
; BEGIN PROBLEM 19
(append (cons (cons 'lambda (cons (car (zip values)) (map let-to-lambda body))) nil) (map let-to-lambda (cadr (zip values))))
; END PROBLEM 19
))
(else
; BEGIN PROBLEM 19
(cons (car expr) (map let-to-lambda (cdr expr)))
; END PROBLEM 19
)))