-
Notifications
You must be signed in to change notification settings - Fork 0
/
questions.scm
92 lines (81 loc) · 2.38 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
(define (caar x) (car (car x)))
(define (cadr x) (car (cdr x)))
(define (cdar x) (cdr (car x)))
(define (cddr x) (cdr (cdr x)))
; Some utility functions that you may find useful to implement.
(define (cons-all first rests)
'replace-this-line)
(define (zip pairs)
'replace-this-line)
;; Problem 17
;; Returns a list of two-element lists
(define (enumerate s)
; BEGIN PROBLEM 17
'replace-this-line
)
; END PROBLEM 17
;; Problem 18
;; List all ways to make change for TOTAL with DENOMS
(define (list-change total denoms)
; BEGIN PROBLEM 18
(define (helper total denoms changes)
(cond
((null? denoms) nil)
((< total (car denoms)) nil)
((= total (car denoms)) (list (append (cons (car denoms) nil) changes)))
(else
(append (helper total (cdr denoms) changes) (helper (- total (car denoms)) denoms (append (cons (car denoms) nil) changes))
)
)
)
)
(helper total (reverse denoms) nil)
)
(define (reverse lis)
(if (null? lis)
'()
(append (reverse (cdr lis))
(list (car lis)))))
(define (cons-all first rests) (map (lambda (n) (cons first n)) rests))
; 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
'replace-this-line
; END PROBLEM 19
)
((quoted? expr)
; BEGIN PROBLEM 19
'replace-this-line
; END PROBLEM 19
)
((or (lambda? expr)
(define? expr))
(let ((form (car expr))
(params (cadr expr))
(body (cddr expr)))
; BEGIN PROBLEM 19
'replace-this-line
; END PROBLEM 19
))
((let? expr)
(let ((values (cadr expr))
(body (cddr expr)))
; BEGIN PROBLEM 19
'replace-this-line
; END PROBLEM 19
))
(else
; BEGIN PROBLEM 19
'replace-this-line
; END PROBLEM 19
)))