- Section 1: Wednesday Sept 19, 18:30
- Section 2: Wednesday Sept 19, 12:30
Homework can submitted via email or printout by deadline.
The former is a list (a linked list of two pairs), the latter is a (single) pair.
(list-length '(eeny meeny miney mo)) --> 4
(list-length '()) --> 0
(define (list-length li)
(if (null? li)
0
(+ 1 (list-length (rest li)))))
(list-append '(mary had a little lamb) '(her fleece was white as snow))
-> '(mary had a little lamb her fleece was white as snow)
(list-append '() '(eeny meeny miney mo))
-> '(eeny meeny miney mo)
(list-append '(eeny meeny miney mo) '())
-> '(eeny meeny miney mo)
(list-append '() '())
-> '()
(define (list-append a b)
(if (null? a)
b
(cons (first a) (list-append (rest a) b))))
(nth 2 '(eeny meeny miney mo)) --> 'meeny (nth 4 '(eeny meeny miney mo)) --> 'mo (nth 5 '(eeny meeny miney mo)) --> '() (nth 0 '(eeny meeny miney mo)) --> '()
(define (nth n li)
(cond ((null? li) '())
((< n 1) '())
((= n 1) (first li))
(else (nth (- n 1) (rest li)))))
(marry '(a b c) '(1 2 3)) --> '((a . 1) (b . 2) (c . 3))
(marry '() '()) --> '()
(define (marry li1 li2)
(if (null? li1)
'()
(cons (cons (first li1) (first li2))
(marry (rest li1) (rest li2)))))
(reverse-list '(a b c d)) --> '(d c b a)
(reverse-list '() '()) --> '()
Hint: You may need to write a "helper function" for
reverse-list to call.
(define (list-reverse li)
(reverse-helper li '()))
(define (reverse-helper li result)
(if (null? li)
result
(reverse-helper (rest li) (cons (first li) result))))