Scheme Programming Assignment Write purely functional Scheme functions to: 1) re
ID: 3829647 • Letter: S
Question
Scheme Programming Assignment
Write purely functional Scheme functions to:
1) return all rotations of a given list. For example, (rotate '(a b c d e)) should return ((a b c d e) (b c d e a) (c d e a b) (d e a b c) (e a b c d)) in some order - not necessarily in the one given in the example.
2) return a list of all elements of a given list that satisfy a given boolean function. For example (filter (lambda (X) ( < X 5)) '(3 9 5 8 2 4 7)) should return (3 2 4).
Comment your code - in Scheme a comment is anything that follows the ";" (semicolon) character on a line. You comments should include your name and the date in addition to any additinal comments.
Explanation / Answer
1) Scheme code :
(define (rotate lst n)
(cond
[(< n 0)
(error "n is negative")]
[(or (= n 0) (null? lst))
lst]
[else
(rotate (append (cdr lst) (list (car lst)))
(- n 1))]))
Implementation :
(rotate '(a b c d e) 3)
=> '(d e a b c)
(rotate '(a b c) 5)
=> '(c a b)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.