Created
January 8, 2016 09:35
-
-
Save anonymous/2e321710134f3d5ab73d to your computer and use it in GitHub Desktop.
guile partition-by
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
(define* (partition-by f l #:optional (p eqv?)) | |
(if (null? l) | |
'() | |
(let A ((L '()) (M (list (car l))) (R (cdr l)) (f1 (f (car l)))) | |
(if (null? R) | |
(reverse (cons (reverse M) L)) | |
(let* ((R1 (car R)) (f2 (f R1)) (R2 (cdr R))) | |
(if (p f1 f2) | |
(A L (cons R1 M) R2 f2) | |
(A (cons (reverse M) L) (list R1) R2 f2))))))) | |
(partition-by | |
(let ((counter 0) (truth #f)) | |
(lambda (x) | |
(if (< counter 5) | |
(begin | |
(set! counter (1+ counter)) | |
truth) | |
(begin | |
(set! counter 1) | |
(set! truth (not truth)) | |
truth)))) | |
'(1 2 3 4 5 6 7 8 9 0 11 12 13 14 15 16 17 18 19 20) eq?) | |
(partition-by | |
identity | |
'(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20) | |
(let ((counter 1)) | |
(lambda (a b) | |
(if (< counter 5) | |
(begin | |
(set! counter (1+ counter)) | |
#t) | |
(begin | |
(set! counter 1) | |
#f))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment