Make filter tail-recursive

This commit is contained in:
koba-e964 2014-06-26 18:40:40 +09:00
parent 7b1cc27b08
commit 5b215eb57a
1 changed files with 2 additions and 6 deletions

View File

@ -402,12 +402,8 @@
;; filter partition remove
;; filter! partition! remove!
(define (filter pred list)
(if (null? list)
'()
(if (pred (car list))
(cons (car list)
(filter pred (cdr list)))
(filter pred (cdr list)))))
(let ((pcons (lambda (v acc) (if (pred v) (cons v acc) acc))))
(reverse (fold pcons '() list))))
(define (remove pred list)
(filter (lambda (x) (not (pred x))) list))