Created
December 12, 2010 06:47
-
-
Save buntine/737890 to your computer and use it in GitHub Desktop.
Snippets for blog article on continuations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(lambda (n) (+ n 1)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c(call/cc f) --> c(f c) ; Not Scheme. c(...) represents the current continuation. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(call/cc | |
(lambda (return) | |
(+ 2 (return 4) 1))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define add-10 #f) | |
(display | |
(+ 10 | |
(call/cc (lambda (k) | |
(set! add-10 k) 0)))) | |
(add-10 20) | |
(add-10 12) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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