chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] Re: sequences egg


From: F. Wittenberger
Subject: Re: [Chicken-users] Re: sequences egg
Date: Mon, 22 Nov 2010 15:03:43 +0100

Am Montag, den 22.11.2010, 03:11 -0500 schrieb Felix:
> > 
> > For a Scheme (who would never ever - promised - participate in a
> > religious war ;-) the file kinda feels like Felix haven read too much CL
> > recently.
> > 
> 
> Yes, that may be, but being able to use a common API over various
> sequences appears to be something that especially newcomers to
> chicken might miss, since most dynamic languages more or less do
> this. It is not meant pedagogically, and I don't even consider it
> a shortcoming of Scheme to chose different names for the same
> operation over different sequence-types. But it still may be
> useful or make things more accessible.

How about stealing most from SRFI-1 like this?

(define (sequence:= first rest empty? = . lists)
  (or (null? lists) ; special case

      (let lp1 ((list-a (car lists)) (others (cdr lists)))
        (or (null? others)
            (let ((list-b (car others))
                  (others (cdr others)))
              (if (eq? list-a list-b)   ; EQ? => LIST=
                  (lp1 list-b others)
                  (let lp2 ((list-a list-a) (list-b list-b))
                    (if (empty? list-a)
                        (and (empty? list-b)
                             (lp1 list-b others))
                        (and (not (empty? list-b))
                             (= (first list-a) (first list-b))
                             (lp2 (rest list-a) (rest list-b)))))))))))

Now SRFI-1's list= would be

(define (list= = . lists)
  (apply sequence:= car cdr null-list? = lists))

;; More wierd
(define (vector:= = . vectors)
  (define (first x) (vector-ref (cdr x) (car x)))
  (define (rest x) (set-car! x (+ (car x) 1)) x)
  (define (empty? x) (eqv? (vector-length (cdr x)) (car x)))
  (apply sequence:= first rest empty= = (map (lambda (v) (cons 0 v))
vectors)))

This would have the advantage, that we have one set of operators to
remember.  And a boilerplate how to create a sequence interface.

Another appealing would be to have a file with definitions like

(define (sequence:= = . lists) ...)

Note, that the first, rest, empty? args are missing.

Those plus some constructors for result sequences would be imported
within the modules, which includes these definitions.  One module per
sequence type.

Together with the suggestion of export prefix stripping (see other
posting), it would we a matter of a pretty simple module clause to
implement a 89 or alike well debugged procedures.

(Actually I'm not sure, whether or not there is an advantage in having
the full code compiled specific to a certain type.  Though this would
probably better for optimisation.  Would it?  But it nevertheless costs
the full code per type.)





reply via email to

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