From 05516c51da2e5489acb224ac6ce467758c15a847 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Mon, 24 Feb 2014 00:50:15 -0800 Subject: [PATCH] list->slices --- list.rkt | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/list.rkt b/list.rkt index 436b71c..89b1558 100644 --- a/list.rkt +++ b/list.rkt @@ -7,10 +7,20 @@ (list? procedure? . -> . list?) (dropf-right (dropf items test-proc) test-proc)) +;; convert a list into a list of slices that are len long (last one might be shorter) +(define+provide/contract (list->slices xs len) + (list? integer? . -> . (listof list?)) + (cond + [(equal? xs null) null] + [(len . > . (length xs)) (list xs)] + [else (cons (take xs len) (list->slices (drop xs len) len))])) + + + ;; split list into list of sublists using test-proc (define+provide/contract (splitf-at* xs split-test) - + ;; todo: better error message when split-test is not a predicate (list? predicate/c . -> . (listof list?)) (define (&splitf-at* xs [acc '()]) ; use acc for tail recursion