PasteRack.org
Paste # 48219
2020-07-08 19:25:32

Fork as a new paste.

Paste viewed 233 times.


Embed:

  1. #lang racket/base
  2.  
  3. (define (split-by f lst (pred? =))
  4.   (reverse
  5.    (let loop ((lst lst) (acc '()))
  6.      (cond
  7.        ((null? lst)
  8.         acc)
  9.        ((null? acc)
  10.         (loop (cdr lst) (list (list (car lst)))))
  11.        ((pred? (f (car lst))
  12.                (f (caar acc)))
  13.         (loop (cdr lst) (cons (cons (car lst) (car acc)) (cdr acc))))
  14.        (else
  15.         (loop (cdr lst) (cons (list (car lst)) acc)))))))

=>