qi icon indicating copy to clipboard operation
qi copied to clipboard

[Feature Request] : Support continuations

Open NoahStoryM opened this issue 2 years ago • 1 comments

I guess capturing continuations in flo is an interesting idea. Assuming Qi supports let/cc form:

(define-qi-syntax-rule (let/cc flo)
  (esc (λ args (let/cc cc (apply (☯ flo) cc args)))))

(define-qi-syntax-rule (let/ec flo)
  (esc (λ args (let/ec ec (apply (☯ flo) ec args)))))

We can use let/cc as loop :

(define (factorial m)
  (define-values (loop n res) (let/cc cc (values cc m 1)))
  (if (zero? n)
      res
      (loop loop (sub1 n) (* n res))))

(define factorial
  (☯
   (~> (let/cc (==* _ _ 1))             ; loop × n × res
       (if (~> 2> zero?)
           3>
           (~> (==* _ (-< _ _) _)       ; loop        × n        × n × res
               (==* (-< _ _) sub1 *)    ; loop × loop × (sub1 n) × (* n res)
               (_ _ _ _))))))

NoahStoryM avatar Aug 17 '22 00:08 NoahStoryM

This came up in some Qi meetings. It seems like it could be interesting to define a core continuation form and then implement try so that it expands to a use of the core continuation form.

cc @dzoep @jairtrejo

countvajhula avatar Jan 26 '24 10:01 countvajhula