Skip to content

Instantly share code, notes, and snippets.

Created January 8, 2016 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/2e321710134f3d5ab73d to your computer and use it in GitHub Desktop.
Save anonymous/2e321710134f3d5ab73d to your computer and use it in GitHub Desktop.
guile partition-by
(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