SHARE
TWEET

Untitled

a guest Mar 22nd, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (interpret
  2.  '(define (generalized-fold knull1? kar1 kdr1 glist1 kons2 knil2)
  3.     (define (lp glist1 glist2)
  4.       (if (knull1? glist1) glist2
  5.           (lp (kdr1 glist1) (kons2 (kar1 glist1) glist2))))
  6.     (lp glist1 knil2)))
  7.  
  8. (interpret '(define next! #f))
  9. (interpret '(define terminate! #f))
  10. (interpret '(define %escape #f))
  11. (interpret '(call/cc (lambda (k) (set! %escape k))))
  12.  
  13. (interpret
  14.  '(define (continued-fraction x)
  15.     (define terminate? #f)
  16.     (define p0 1) (define p1 0) (define q0 0) (define q1 1)
  17.     (set! terminate! (lambda () (set! terminate? #t) (next!)))
  18.  
  19.     (generalized-fold
  20.      (lambda (_) terminate?)
  21.      (lambda (x)
  22.        (define x0 (inexact->exact (floor x)))
  23.        (call/cc (lambda (k2)
  24.                   (define p2 (+ (* x0 p1) p0)) (define q2 (+ (* x0 q1) q0))
  25.                   (set! p0 p1) (set! q0 q1) (set! p1 p2) (set! q1 q2)
  26.                   (set! next! k2)
  27.                   (%escape (string->symbol {[q1]/[p1]})))) ; JScheme's quasi-string
  28.        x0)
  29.      (lambda (x) (/ 1 (- x (floor x))))
  30.      x cons '())))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top