Skip to content

Instantly share code, notes, and snippets.

@buntine
Created December 12, 2010 06:47
Show Gist options
  • Save buntine/737890 to your computer and use it in GitHub Desktop.
Save buntine/737890 to your computer and use it in GitHub Desktop.
Snippets for blog article on continuations
(lambda (n) (+ n 1))
c(call/cc f) --> c(f c) ; Not Scheme. c(...) represents the current continuation.
(call/cc
(lambda (return)
(+ 2 (return 4) 1)))
(define (has-sym? lst s)
(cond
((empty? lst) #f) ; Nothing left, must not be present.
((list? (car lst)) ; Is a sublist, inspect it also.
(or (has-sym? (car lst) s)
(has-sym? (cdr lst) s)))
((eq? s (car lst)) #t) ; Found a match, return true to the caller!
(else
(has-sym? (cdr lst) s)))) ; Nothing yet, keep looking...
(define (has-sym? lst s)
(call/cc
(lambda (return)
(define (find lst) ; Helper proc, local to lambda argument of call/cc.
(cond
((empty? lst) #f)
((list? (car lst))
(or (find (car lst))
(find (cdr lst))))
((eq? s (car lst))
(return #t)) ; A match! Invoke 'return' for an early-escape!
(else
(find (cdr lst)))))
(find lst)))) ; We end up here when/if 'return' is invoked.
(define add-10 #f)
(display
(+ 10
(call/cc (lambda (k)
(set! add-10 k) 0))))
(add-10 20)
(add-10 12)
(define (fact n)
(let* ((total n)
(k (call/cc (lambda (kk) kk))))
(set! n (- n 1))
(set! total (* total n))
(if (<= n 1)
total
(k k))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment