[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] continuation example: different behavior from other
From: |
megane |
Subject: |
Re: [Chicken-users] continuation example: different behavior from other Scheme implementations |
Date: |
Sat, 19 Oct 2019 16:46:18 +0300 |
User-agent: |
mu4e 1.0; emacs 25.1.1 |
Ricardo Gabriel Herdt <address@hidden> writes:
> Am 19.10.2019 14:18 schrieb address@hidden:
>> The exact behaviour of re-
>> entering a continuation captured during execution of "map" is, I think,
>> dependent on implementation details (there may be a note about this in
>> the
>> SRFI-1 document or R7RS, I can't remember right now).
>
> Many thanks for the fast and detailed answer.
>
> I found this in R5RS/R7RS/SRFI-1: "The dynamic order in which proc is
> applied to the elements of the lists is unspecified". Indeed redefining
> map in all implementations as
>
> (define (map f l)
> (cond ((null? l) '())
> (else (cons (f (car l))
> (map f (cdr l))))))
>
> make all behave the same way (I know this is not a complete map
> implementation, is just an example).
>
> R7RS adds this: "If multiple returns occur from map, the values returned
> by earlier returns are not mutated". Does this mean returns through
> continuation calls?
That's how I understand it.
>
> I was curious how CHICKEN implements map and found the following in
> library.scm:
>
> (define (##sys#map p lst0)
> (let loop ((lst lst0))
> (cond ((eq? lst '()) lst)
> ((pair? lst)
> (cons (p (##sys#slot lst 0)) (loop (##sys#slot lst 1))) )
> (else (##sys#error-not-a-proper-list lst0 'map)) ) ))
>
> ...
> (set! scheme#map
> (lambda (fn lst1 . lsts)
> (if (null? lsts)
> (##sys#map fn lst1)
> (let loop ((all (cons lst1 lsts)))
> (let ((first (##sys#slot all 0)))
> (cond ((pair? first)
> (cons (apply fn (mapsafe (lambda (x) (car x)) all #t 'map))
> (loop (mapsafe (lambda (x) (cdr x)) all #t 'map)) ) )
> (else (check (##core#inline "C_i_cdr" all) #t 'map)
> '() ) ) ) ) ) ) )
> ...
>
> So still I don't get why calling any stored continuation appends the
> result to the previously computed "numbers", but if the standard allows
> this behavior it's not a big deal.
There's special compiler syntax for map; search for 'map-loop in the
sources. You can also see what your code expands to by giving -debug 2
flag to csc.
>
> Regards,
>
> Ricardo
>
>
> _______________________________________________
> Chicken-users mailing list
> address@hidden
> https://lists.nongnu.org/mailman/listinfo/chicken-users