chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: filter


From: Lassi Kortela
Subject: Re: filter
Date: Thu, 28 Jan 2021 19:00:55 +0200
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0) Gecko/20100101 Thunderbird/78.6.1

I suggest, to include a filter routine with two values, the sublist
which passes the test and the sublist which fails

In Scheme that procedure is called `partition`, also from SRFI 1:
https://srfi.schemers.org/srfi-1/srfi-1.html#partition

`filter` returns one list containing the values that pass the test:
https://srfi.schemers.org/srfi-1/srfi-1.html#filter

You can define `partition` in a few lines:

(define (partition pass? xs)
  (let loop ((xs xs) (pass '()) (fail '()))
    (if (null? xs) (values (reverse pass) (reverse fail))
        (if (pass? (car xs))
            (loop (cdr xs) (cons (car xs) pass) fail)
            (loop (cdr xs) pass (cons (car xs) fail))))))

(partition even? '(0 1 2 3 4 5 6 7 8 9)) => (0 2 4 6 8); (1 3 5 7 9)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]