Watching this video (11:56)
It shows a recursive procedure that multiplies the numbers contained in a list
The idea is that if the list contains a zero, the whole stack of recursive calls can be discarded and 0 can be returned
So to save some multiplications
It does so by early exiting the procedure with delimited continuations
I'd like to reproduce this in Guile Scheme and I wrote this code
(define (times numbers)
(define (times-iter numbers)
(match numbers
((a-number rest ...)
(* a-number (times rest)))
('() 1)
((0 rest ...) (shift k 0) )
))
(reset (times-iter numbers))
)
it multiplies correctly but if I pass it a list containing a zero and I trace such call, I get
scheme@(guile-user)> ,trace (times2 '(1 3 0 4))
trace: | (times2 (1 3 0 4))
trace: | | (default-prompt-tag@@ice-9/control)
trace: | | (_)
trace: | | ("prompt")
trace: | | (_)
trace: | | | (list? (3 0 4))
trace: | | | #t
trace: | | | (times (3 0 4))
trace: | | | | (list? (0 4))
trace: | | | | #t
trace: | | | | (times (0 4))
trace: | | | | | (list? (4))
trace: | | | | | #t
trace: | | | | | (times (4))
trace: | | | | | | (list? ())
trace: | | | | | | #t
trace: | | | | | | (times ())
trace: | | | | | | 1
trace: | | | | | 4
trace: | | | | 0
trace: | | | 0
trace: | | 0
trace: | (_ #<procedure values _> (0))
trace: | (_ 0)
trace: | 0
scheme@(guile-user)>
It seems to me that the early exit doesn't kick in and the whole stack of multiplications gets applied
What am I doing wrong ?