Advertisement
kenpusney

grouping.rkt

Dec 23rd, 2021 (edited)
1,901
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 0.70 KB | None | 0 0
  1. #lang racket
  2. (require rackunit)
  3.  
  4. (define (grouping values pred [acc '()])
  5.   (if (empty? values)
  6.       acc
  7.       (let-values ([(taken rest) (splitf-at values pred)])
  8.         (grouping
  9.          (dropf rest (negate pred))
  10.          pred
  11.          (append acc (list taken))))))
  12.  
  13. (check-equal? '()
  14.               (grouping '() number?))
  15.  
  16. (check-equal? '((1 2 3))
  17.               (grouping '(1 2 3) number?))
  18.              
  19.  
  20. (check-equal? '((1 2 3) (4 5 6))
  21.            (grouping '(1 2 3 null 4 5 6) number?))
  22.  
  23. (check-equal? '(("a" "b" "c") ("d" "e" "f"))
  24.            (grouping (list
  25.                       "a" "b" "c"
  26.                       ""
  27.                       "d" "e" "f") non-empty-string?))
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement