You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sugar/tree.rkt

32 lines
798 B
Racket

11 years ago
#lang racket/base
11 years ago
(require "define/contract.rkt")
11 years ago
(module+ test (require rackunit))
(define+provide/contract (filter-tree proc tree)
(procedure? list? . -> . list?)
(define (remove-empty x)
(cond
11 years ago
[(list? x) (filter (compose1 not null?) (map remove-empty x))]
11 years ago
[else x]))
(define (filter-tree-inner proc x)
(cond
[(list? x) (map (λ(i) (filter-tree-inner proc i)) x)]
11 years ago
[else (if (proc x) x null)]))
11 years ago
(remove-empty (filter-tree-inner proc tree)))
11 years ago
11 years ago
(define+provide/contract (filter-not-tree proc tree)
(procedure? list? . -> . list?)
(filter-tree (λ(i) (not (proc i))) tree))
(define+provide/contract (map-tree proc tree)
(procedure? list? . -> . list?)
(cond
[(list? tree) (map (λ(i) (map-tree proc i)) tree)]
[else (proc tree)]))