Quantcast
Channel: Comments on: Continuation kata
Viewing all articles
Browse latest Browse all 10

By: Kevin Greer

$
0
0
Below is my solution: <pre> (define fail '()) (call/cc (lambda (k) (set! fail k))) (define (in-range-or-else s e else) (if (> s e) (begin (set! fail else) (else)) (call/cc (lambda (k) (set! fail (lambda () (k (in-range-or-else (+ s 1) e else)))) s)))) (define (in-range s e) (in-range-or-else s e fail)) </pre> I used an internal function in my solution but because I thought it might be useful on its own, I've exposed it. It is called in-range-or-else and it lets you provide the function to call on failure. The in-range function just calls this with 'fail'. Here's a similiar function called "in-list": <pre> (define (in-list-or-else l else) (if (null? l) (begin (set! fail else) (else)) (call/cc (lambda (k) (set! fail (lambda () (k (in-list-or-else (cdr l) else)))) (car l))))) (define (in-list l) (in-list-or-else l fail)) </pre>

Viewing all articles
Browse latest Browse all 10

Trending Articles