l-99

solutions to l-99 (slow)
Log | Files | Refs | Feed | README

commit b4cd4f81bdd64079152b432f8677ba1cbb3a8626
parent 2933399663d5ae36c6ea67d9e2f1185d8dbde328
Author: Jenny Doe <tng@soykaf.me>
Date:   Thu,  2 May 2019 14:15:29 +0200

added: p19.scm

Diffstat:
Ap19.scm | 23+++++++++++++++++++++++
1 file changed, 23 insertions(+), 0 deletions(-)

diff --git a/p19.scm b/p19.scm @@ -0,0 +1,23 @@ +; (**) Rotate a list N places to the left. +; Examples: +; * (rotate '(a b c d e f g h) 3) +; (D E F G H A B C) +; +; * (rotate '(a b c d e f g h) -2) +; (G H A B C D E F) +; +; Hint: Use the predefined functions length and append, as well as the +; result of problem P17. + +(load "p17.scm") + +(define (rotate ls n) + (let ((n (mod n (length ls)))) + (cond + ((= n 0) ls) + ((> n 0) + (let ((ls (split ls n))) + (append (cadr ls) (car ls)))) + ((< n 0) + (let ((ls (split ls (+ (length ls) n)))) + (append (cadr ls) (car ls)))))))