X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=hints%2Fcps_hint_2;fp=hints%2Fcps_hint_2;h=0000000000000000000000000000000000000000;hp=2cdc49ae1beda57a423d960ce4c28ef999bd3e6f;hb=4fde59e8440a11db16396496648d9b53fe055670;hpb=51ee06f53535b62965d8f34dbc4ab2456afe73c5 diff --git a/hints/cps_hint_2 b/hints/cps_hint_2 deleted file mode 100644 index 2cdc49ae..00000000 --- a/hints/cps_hint_2 +++ /dev/null @@ -1,43 +0,0 @@ -This function is developed in *The Seasoned Schemer* pp. 76-83. It accepts a list `lst` and returns the leftmost atom in it, even if that atom is embedded several levels deep. Any empty lists preceding the leftmost atom are ignored. - - - #lang racket - - (define (atom? x) - (and (not (pair? x)) (not (null? x)))) - - (define beta - (lambda (lst) - (let/cc k ; calling k with val will immediately return val from the call to beta - (letrec ([aux (lambda (l) - (cond - [(null? l) '()] - [(atom? (car l)) (k (car l))] - [else (begin - ; each of the following lines will evaluate to '() iff no atom was found in the specified part of l - (aux (car l)) - (aux (cdr l)))]))]) - (aux lst))))) - - (beta '(((a b) ()) (c (d ())))) ; ~~> 'a - (beta '((() (a b) ()) (c (d ())))) ; ~~> 'a - (beta '(() (() (a b) ()) (c (d ())))) ; ~~> 'a - (beta '(() (() ()))) ; no leftmost atom, returns '() - -This function could also be written like this: - - (define leftmost - (lambda (l) - (cond - [(null? l) '()] - [(atom? (car l)) (car l)] - [else (let ([found (leftmost (car l))]) - (cond - ; here we check whether the recursive call found an atom in (car l) - [(atom? found) found] - ; if not, we search for an atom in (cdr l) - [else (leftmost (cdr l))]))]))) - -But in this version, when an atom is found, it is returned back the chain of recursive calls, one by one. The previous version, on the other hand, uses a captured continuation `k` to return the atom immediately upon finding it. - -