Skip to content

Instantly share code, notes, and snippets.

@themattchan
Last active August 29, 2015 14:22
Show Gist options
  • Save themattchan/7e389942449ea72abe24 to your computer and use it in GitHub Desktop.
Save themattchan/7e389942449ea72abe24 to your computer and use it in GitHub Desktop.
#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