{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

sample_test1_sol

# sample_test1_sol - nil 6 Answer a Yes any list of naturals...

This preview shows pages 1–2. Sign up to view the full content.

Test Exam -- Midterm 1 -- Solutions: 1) Answer: t 2) Answer: (implies (and (equal a t) (equal b t)) (or (equal x 5) (equal y 5))) 3) Answer: You need to decide how to handle the possibility that one list is shorter than the other. E.g., perhaps (zip '(a b) '(1 2 3)) ==> ((a 1) (b 2)) [stops when 1st arg exhausted] or ==> ((a 1) (b 2) (nil 3)) [stops when 2nd arg exhausted and takes first and rest of nil] We go with the former implementation: (defun zzip (x y) (if (endp x) nil (if (endp y) nil (cons (cons (first x) (cons (first y) nil) ) (zzip (rest x) (rest y)) ) ) ) ) 4) Answer: (defun del-nats (x) (if (consp x) (if (natp (first x)) (del-nats (rest x)) (cons (first x) (del-nats (rest x)) ) ) nil ) ) 5) Answer: (defun collect-nats (x) (if (consp x) (if (natp (first x)) (cons (first x) (collect-nats (rest x)) ) (collect-nats (rest x))

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ) nil ) ) 6) Answer: a) Yes, any list of naturals such as (), (1), (4 5 8 10) . .. b) Yes, ((1 2) 1 a 2 b) c) Yes, (a 1 2) 7) Answer: (defun collect-dups (x) (if (consp x) (if (mem (first x) (rest x)) (cons (first x) (collect-dups (del* (first x) (rest x)) ) ) (collect-dups (rest x)) ) nil ) ) Here I used (del* e x) from Q88 By the way, this function might also be defined in a way that collects A twice because it appears 3 times: (collect-dups '(a b c a b a d e f)) ==> (a b a) 8) (defun find-pos-n (a x n) (if (endp x) n (if (equal a (first x)) n (find-pos-n a (rest x) (+ n 1)) ) ) ) (defun fine-pos (a x) (find-pos-n (a x 0) ) 9) Answer: (implies (or (e mem x) (e mem x) ) (e mem (app x y)) ) Here I used functions (app x y) from Q89 and (mem e x) from Q84....
View Full Document

{[ snackBarMessage ]}

### Page1 / 2

sample_test1_sol - nil 6 Answer a Yes any list of naturals...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online