Last active
August 29, 2015 14:22
-
-
Save themattchan/7e389942449ea72abe24 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#lang racket | |
(require test-engine/racket-tests) | |
(define (rotations lst) | |
(letrec ((go (λ (n acc) | |
(if (= n 0) acc | |
(go (sub1 n) (let ((c (car acc))) | |
`(,`(,@(cdr c) ,(car c)) ,@acc))))))) | |
(reverse (go (length lst) (list lst))))) | |
(define (sliding-window n lst) | |
(apply map list (take (rotations lst) n))) | |
(check-expect (sliding-window 3 `(1 2 3 4 5)) `((1 2 3) (2 3 4) (3 4 5) (4 5 1) (5 1 2))) | |
(test) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment