Created
August 3, 2012 20:01
-
-
Save takikawa/3250962 to your computer and use it in GitHub Desktop.
group
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
#lang racket | |
;; splits the list into groups of n or less | |
;; nat (listof x) -> (listof (listof x)) | |
(define (group n lst) | |
(let loop ([lst lst] [acc '()]) | |
(if (null? lst) | |
(reverse acc) | |
(loop (drop-up-to lst n) | |
(cons (take-up-to lst n) acc))))) | |
;; take n if available, otherwise all | |
;; (listof x) nat -> (listof x) | |
(define (take-up-to lst n) | |
(if (< (length lst) n) | |
lst | |
(take lst n))) | |
;; drop n if available, otherwise none | |
;; (listof x) nat -> (listof x) | |
(define (drop-up-to lst n) | |
(if (< (length lst) n) | |
null | |
(drop lst n))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment