Starting from:
$30

$24

Assignment 6 Solution

Problem I { Scheme




Write Scheme programs that generate the following lists as output using only cons as the list building operator:




‘((c (e f (b))) g b)



‘((g) ((((h) i)) (a f)) d)



A list l that contains three items: the atom ’g’, the division operator ’/’, and the atom ’6’ such that ((cadr l) 18 9) evaluates to 2.



Note that cadr is composed of car and cdr such that (cadr l) = (car (cdr l)).







Problem II { Scheme




Write the following functions on lists in Scheme. The semantics of the func-tions is described through examples.




1. Get nth digit of an integer




(define getnthdigit




(lambda (m n)




...))




...




(getnthdigit 32145 1) -- 5




(getnthdigit 32145 2) -- 4










Note: You can use Scheme build-in function "modulo" and " oor".
















1















(define rev (lambda (l)



...))




...




(rev ‘(e((b)(c d)(((f)))))) -- ‘(((((f)))(d c)(b))e)







Note: Do not use the Scheme build-in function "reverse".




3. Position of rst occurrence of k in list




(define positionof




(lambda (k lst)




...))




...




(positionof ‘c ‘(c b d c e f)) -- 1










Note: You may want to de ne a helper function



























































































2

More products