-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestions.scm
79 lines (68 loc) · 2.21 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
(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 15
;; Returns a list of two-element lists
(define (iota n)
(if (= n 0) '() (append (iota (- n 1)) `(,(- n 1)))))
(define (zip_ seq1 seq2)
(if (null? seq1) '() (cons `(,(car seq1) ,(car seq2)) (zip_ (cdr seq1) (cdr seq2)))))
(define (enumerate s)
; BEGIN PROBLEM 15
(zip_ (iota (length s)) s))
; END PROBLEM 15
;; Problem 16
;; Merge two lists S1 and S2 according to ORDERED? and return
;; the merged lists.
(define (merge ordered? s1 s2)
; BEGIN PROBLEM 16
(cond ((null? s1) s2)
((null? s2) s1)
((ordered? (car s1) (car s2)) (cons (car s1) (merge ordered? (cdr s1) s2)))
(else (cons (car s2) (merge ordered? s1 (cdr s2))))))
; END PROBLEM 16
;; Optional Problem 2
;; 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 OPTIONAL PROBLEM 2
'replace-this-line
; END OPTIONAL PROBLEM 2
)
((quoted? expr)
; BEGIN OPTIONAL PROBLEM 2
'replace-this-line
; END OPTIONAL PROBLEM 2
)
((or (lambda? expr)
(define? expr))
(let ((form (car expr))
(params (cadr expr))
(body (cddr expr)))
; BEGIN OPTIONAL PROBLEM 2
'replace-this-line
; END OPTIONAL PROBLEM 2
))
((let? expr)
(let ((values (cadr expr))
(body (cddr expr)))
; BEGIN OPTIONAL PROBLEM 2
'replace-this-line
; END OPTIONAL PROBLEM 2
))
(else
; BEGIN OPTIONAL PROBLEM 2
'replace-this-line
; END OPTIONAL PROBLEM 2
)))
; Some utility functions that you may find useful to implement for let-to-lambda
(define (zip pairs)
'replace-this-line)