Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Scheme Programming Write purely functional Scheme functions to: 1) return all ro

ID: 3832536 • Letter: S

Question

Scheme Programming

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.

Explanation / Answer

1)

(define rotate

(lambda (ls)

(define subrotate

(lambda (head tail res)

(if (null? tail)

(reverse res) ; modified

(subrotate (append head (list (car tail)))

(cdr tail)

(cons (append tail head) res)))))

(if (null? ls)

ls

(subrotate '() ls '()))))

2)

(define filterb

(lambda (pred lst)

(cond ((null? lst) '())

((pred (car lst)) (cons (car lst) (filterb pred (cdr lst))))

(else (filterb pred (cdr lst))))))

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote