|
|
@ -13,11 +13,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
(define/typed+provide (slicef xs pred)
|
|
|
|
(define/typed+provide (slicef xs pred)
|
|
|
|
(All (A) ((Listof A) (A -> Boolean) -> (Listof (Listof A))))
|
|
|
|
(All (A) ((Listof A) (A -> Boolean) -> (Listof (Listof A))))
|
|
|
|
(let loop ([xs xs][acc : (Listof (Listof A)) empty])
|
|
|
|
(define-values (last-list list-of-lists last-negating)
|
|
|
|
(if (empty? xs)
|
|
|
|
(for/fold: ([current-list : (Listof A) empty]
|
|
|
|
(reverse acc)
|
|
|
|
[list-of-lists : (Listof (Listof A)) empty]
|
|
|
|
(let-values ([(cdr-matches rest) (splitf-at (cdr xs) (if (pred (car xs)) pred (λ([x : A]) (not (pred x)))))])
|
|
|
|
[negating? : Boolean #f])
|
|
|
|
(loop rest (cons (cons (car xs) cdr-matches) acc))))))
|
|
|
|
([x (in-list xs)])
|
|
|
|
|
|
|
|
(define current-pred (if negating? (λ: ([x : A]) (not (pred x))) pred))
|
|
|
|
|
|
|
|
(if (current-pred x)
|
|
|
|
|
|
|
|
(values (cons x current-list) list-of-lists negating?)
|
|
|
|
|
|
|
|
(values (cons x null) (if (not (empty? current-list))
|
|
|
|
|
|
|
|
(cons (reverse current-list) list-of-lists)
|
|
|
|
|
|
|
|
list-of-lists) (not negating?)))))
|
|
|
|
|
|
|
|
(reverse (cons (reverse last-list) list-of-lists)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define/typed+provide slicef-at
|
|
|
|
(define/typed+provide slicef-at
|
|
|
|
;; with polymorphic function, use cased typing to simulate optional position arguments
|
|
|
|
;; with polymorphic function, use cased typing to simulate optional position arguments
|
|
|
@ -29,8 +37,8 @@
|
|
|
|
[(xs pred force?)
|
|
|
|
[(xs pred force?)
|
|
|
|
(define-values (last-list list-of-lists)
|
|
|
|
(define-values (last-list list-of-lists)
|
|
|
|
(for/fold:
|
|
|
|
(for/fold:
|
|
|
|
([current-list : (Listof A) empty][list-of-lists : (Listof (Listof A)) empty])
|
|
|
|
([current-list : (Listof A) empty][list-of-lists : (Listof (Listof A)) empty])
|
|
|
|
([x (in-list xs)])
|
|
|
|
([x (in-list xs)])
|
|
|
|
(if (pred x)
|
|
|
|
(if (pred x)
|
|
|
|
(values (cons x null) (if (not (empty? current-list))
|
|
|
|
(values (cons x null) (if (not (empty? current-list))
|
|
|
|
(cons (reverse current-list) list-of-lists)
|
|
|
|
(cons (reverse current-list) list-of-lists)
|
|
|
@ -47,7 +55,7 @@
|
|
|
|
(All (A) ((Listof A) (A -> Boolean) -> (Listof (Listof A))))
|
|
|
|
(All (A) ((Listof A) (A -> Boolean) -> (Listof (Listof A))))
|
|
|
|
(define-values (last-list list-of-lists)
|
|
|
|
(define-values (last-list list-of-lists)
|
|
|
|
(for/fold: ([current-list : (Listof A) empty][list-of-lists : (Listof (Listof A)) empty])
|
|
|
|
(for/fold: ([current-list : (Listof A) empty][list-of-lists : (Listof (Listof A)) empty])
|
|
|
|
([x (in-list xs)])
|
|
|
|
([x (in-list xs)])
|
|
|
|
(if (pred x)
|
|
|
|
(if (pred x)
|
|
|
|
(values empty (cons (reverse (cons x current-list)) list-of-lists))
|
|
|
|
(values empty (cons (reverse (cons x current-list)) list-of-lists))
|
|
|
|
(values (cons x current-list) list-of-lists))))
|
|
|
|
(values (cons x current-list) list-of-lists))))
|
|
|
@ -66,7 +74,7 @@
|
|
|
|
[(xs len force?)
|
|
|
|
[(xs len force?)
|
|
|
|
(define-values (last-list list-of-lists)
|
|
|
|
(define-values (last-list list-of-lists)
|
|
|
|
(for/fold: ([current-list : (Listof A) empty][list-of-lists : (Listof (Listof A)) empty])
|
|
|
|
(for/fold: ([current-list : (Listof A) empty][list-of-lists : (Listof (Listof A)) empty])
|
|
|
|
([x (in-list xs)][i (in-naturals)])
|
|
|
|
([x (in-list xs)][i (in-naturals)])
|
|
|
|
(if (= (modulo (add1 i) len) 0)
|
|
|
|
(if (= (modulo (add1 i) len) 0)
|
|
|
|
(values empty (cons (reverse (cons x current-list)) list-of-lists))
|
|
|
|
(values empty (cons (reverse (cons x current-list)) list-of-lists))
|
|
|
|
(values (cons x current-list) list-of-lists))))
|
|
|
|
(values (cons x current-list) list-of-lists))))
|
|
|
@ -79,7 +87,7 @@
|
|
|
|
(All (A) ((Listof A) (A -> Boolean) -> (Listof (Listof A))))
|
|
|
|
(All (A) ((Listof A) (A -> Boolean) -> (Listof (Listof A))))
|
|
|
|
(define-values (last-list list-of-lists)
|
|
|
|
(define-values (last-list list-of-lists)
|
|
|
|
(for/fold: ([current-list : (Listof A) empty][list-of-lists : (Listof (Listof A)) empty])
|
|
|
|
(for/fold: ([current-list : (Listof A) empty][list-of-lists : (Listof (Listof A)) empty])
|
|
|
|
([x (in-list xs)])
|
|
|
|
([x (in-list xs)])
|
|
|
|
(if (pred x)
|
|
|
|
(if (pred x)
|
|
|
|
(values empty (if (not (empty? current-list))
|
|
|
|
(values empty (if (not (empty? current-list))
|
|
|
|
(cons (reverse current-list) list-of-lists)
|
|
|
|
(cons (reverse current-list) list-of-lists)
|
|
|
|