Loading [MathJax]/extensions/tex2jax.js

2010年7月15日木曜日

Rotate a list N places to the left

;; P19 (**) 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.
(require "p17.ss")
(define (rotate lst x)
(define (proc arg)
(if (negative? x)
(reverse arg)
arg))
(let ((ls (split (proc lst) (abs x))))
(let ((head (car ls)) (tail (cadr ls)))
(proc (append tail head)))))
view raw p19.ss hosted with ❤ by GitHub

0 件のコメント:

コメントを投稿