/ Published in: Scheme
Function group-for which return list of list of length n.
(group-for '(1 2 3 4 5 6 7 8 9) 3)
=> ((1 2 3) (4 5 6) (7 8 9))
(group-for '(1 2 3 4 5 6 7 8 9) 3)
=> ((1 2 3) (4 5 6) (7 8 9))
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
(define (skip list n) (cond ((null? list) '()) ((<= n 0) list) (else (skip (cdr list) (- n 1))))) (define (splice list n m) (let loop ((stop (- m n)) (list (skip list n))) (if (or (eq? stop 0) (null? list)) '() (cons (car list) (loop (- stop 1) (cdr list)))))) (define (group-for list count) (let ((step (/ (length list) count))) (let loop ((i 0)) (if (eq? i count) '() (cons (splice list (* i count) (* (+ i 1) count)) (loop (+ i 1)))))))