[racket] a small programming exercise

From: namekuseijin (namekuseijin at gmail.com)
Date: Thu Oct 14 13:30:25 EDT 2010

On Thu, Oct 14, 2010 at 2:10 PM, Vincent St-Amour <stamourv at ccs.neu.edu> wrote:
> At Thu, 14 Oct 2010 13:56:16 -0300,
> namekuseijin wrote:
>> (all-d     (filter ns (lambda (n)      (char=? n d))))
>> (all-but-d (filter ns (lambda (n) (not (char=? n d))))))
> Sounds like a job for partition.
> http://doc.racket-lang.org/reference/pairs.html?q=partition#%28def._%28%28lib._racket/list..rkt%29._partition%29%29

yes, after I've gone through this algorithm, I devised a version using
partition instead.  Here's mine, in portable scheme:

(define (partition f ls)
  (if (null? ls) (pair ls ls)
    (let ((p (partition f (rest ls))))
       (if (f ls)
          (pair (pair (first ls) (first p))
                (rest p))
          (pair (first p)
                (pair (first ls) (rest p)))))))

in single-threaded strict scheme, it should be faster than the version
I provided, since it iterates the list at once for both results.

1) my code above is purely functional and providing a scheme
implementation would take advantage of that, it could process both let
clauses in parallel
2) filter makes it more readable and immediately understandable than partition

Posted on the users mailing list.