diff --git a/list.rkt b/list.rkt index 6ff3472..d1186b4 100644 --- a/list.rkt +++ b/list.rkt @@ -11,14 +11,13 @@ (define+provide/contract (slicef-at xs pred [force? #f]) ((list? procedure?) (boolean?) . ->* . list-of-lists?) - (define-values (last-list list-of-lists _) - (for/fold ([current-list empty][list-of-lists empty][previous-x-was-pred #f])([x (in-list xs)]) - (define pred-x (pred x)) - (if (and (not previous-x-was-pred) pred-x) + (define-values (last-list list-of-lists) + (for/fold ([current-list empty][list-of-lists empty])([x (in-list xs)]) + (if (pred x) (values (cons x null) (if (not (empty? current-list)) (cons (reverse current-list) list-of-lists) - list-of-lists) pred-x) - (values (cons x current-list) list-of-lists pred-x)))) + list-of-lists)) + (values (cons x current-list) list-of-lists)))) (let ([list-of-lists (reverse (if (empty? last-list) list-of-lists (cons (reverse last-list) list-of-lists)))]) diff --git a/scribblings/list.scrbl b/scribblings/list.scrbl index 54b6a52..dafc818 100644 --- a/scribblings/list.scrbl +++ b/scribblings/list.scrbl @@ -64,6 +64,7 @@ Divide @racket[_lst] into sublists starting with elements matching @racket[_pred @examples[#:eval my-eval (slicef-at (range 5) even?) +(slicef-at '(1 2 2 1 2) even?) (slicef-at (range 5) odd?) (slicef-at (range 5) odd? #t)] diff --git a/tests.rkt b/tests.rkt index 168809a..5343f34 100644 --- a/tests.rkt +++ b/tests.rkt @@ -157,7 +157,8 @@ (check-equal? (slicef-at (range 5) procedure?) '((0 1 2 3 4))) (check-exn exn:fail:contract? (λ() (slicef-at (range 5) 3))) -(check-equal? (slicef-at '(1 2) integer?) '((1 2))) +(check-equal? (slicef-at '(1 2 2 1 2) even?) '((1) (2) (2 1) (2))) +(check-equal? (slicef-at '(1 2 2 1 2) even? #t) '((2) (2 1) (2))) (check-equal? (sublist (range 5) 0 0) '()) (check-equal? (sublist (range 5) 0 1) '(0))