This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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))))) |
0 件のコメント:
コメントを投稿