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

Re: [Scheme-reports] multiple returns from map functions?

On Thu, 05 May 2011 14:48:40 -0400, Andy Wingo <wingo@x> wrote:

> On Thu 05 May 2011 19:39, "Aaron W. Hsu" <arcfide@x> writes:
>> On Thu, 05 May 2011 11:43:53 -0400, <xacc.ide@x> wrote:
>>> The brings up the question whether list control procedures can accept
>>> cyclic
>>> lists.
>>> Should the following be valid?
>> This is interesting, normally, `map' is specifically not allowed to take
>> circular lists and you get an error when trying to deal with them.
>> However, that is partly because of the restriction prior to this that  
>> all
>> arguments should be of the same length. Now, I think, there is an
>> opportunity to consider `map' in different terms. Whether we should do
>> this or not is still up to question.
> I don't know either, but do see srfi-1.

Here's a naive one argument map form that actually does handle circular  
lists. It's probably not very clean, but it works.

> (define (map f lst)
     (let ([circular #f])
       (define (loop t r)
           [(null? t) '()]
           [(null? r)
            (cons (f (car t))
              (loop (cdr t) r))]
           [(null? (cdr r))
            (cons (f (car t))
              (loop (cdr t) '()))]
           [(eq? t r)
            (let ([res (list (f (car t)) (f (cadr t)))])
              (set! circular (cdr res))
           [else (cons (f (car t)) (loop (cdr t) (cddr r)))]))
         [(null? lst) '()]
         [(null? (cdr lst)) (list (f (car lst)))]
           (let ([res (loop lst (cddr lst))])
             (when circular (set-cdr! circular res))
> (let ([x (iota 10)])
     (set-cdr! (list-tail x 9) x)
     (map (lambda (x) (1+ x)) x))
Warning in pretty-print: cycle detected; proceeding with (print-graph #t)
#0=(1 2 3 4 5 6 7 8 9 10 . #0#)

	Aaron W. Hsu

Programming is just another word for the lost art of thinking.

Scheme-reports mailing list