CS314-Fall-2018-Assign6-Solution
1 Problem I
1.1
(cons
(cons ‘c
(cons
(cons ‘e (cons ‘f
(cons (cons ‘b ‘()) ‘())
)
) ‘()))
(cons ‘g (cons ‘b ‘()))
)
1.2
(cons
(cons ‘g ‘())
(cons
(cons
(cons
(cons (cons ‘h ‘()) (cons ‘i ‘()))
‘())
(cons
(cons ‘a (cons ‘f ‘()))
‘())
)
(cons ‘d ‘())
)
)
1.3
(cons ‘g (cons / (cons 6 ‘())))
1
2 Problem II
2.1
(define getnthdigit
(lambda (m n)
(if (equal? 1 n)
(modulo m 10)
(getnthdigit (floor (/ m 10)) (- n 1))
)
)
)
2.2
(define rev
(lambda (l)
(cond
((null? l) ‘())
((list? (car l)) (append (rev (cdr l))
(cons (rev (car l)) ‘())))
(else (append (rev (cdr l)) (cons (car l) ‘())))
)
)
)
2.3
(define positionofhelp
(lambda (k lst m)
(if (null? lst) 0
(if (equal? k (car lst)) m
(positionofhelp k (cdr lst) (+ 1 m))
)
)
)
)
(define positionof
(lambda (k lst)
(positionofhelp k lst 1)
)
)
2