s c h e m a t i c s : c o o k b o o k

/ Cookbook.SucessFailContinuations

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

How to Succeed and Fail with Continuations

You need to walk a data structure, and the nodes you visit determine what to do. A simple example is to search a rose-tree for a given value. A more complex example is accumulate a set of bindings based on the nodes you encounter.

Use continuations! Pass in two stub procedures (succ, fail) that know what to do in the base case, and extend those procedures at each successive stage. Then, tail-call those procedures when needed -- ie. call success with the accumulator, or fail with nothing.

Problem

Compute a procedure that generates the accessor for the (first) matching symbol in a DFS search of a rose-tree of symbols.

Solution

(define search
  (lambda (sym tree)
    (search-path sym
                 tree
                 (lambda (path) path) ;success
                 (lambda ()           ;failure
                   (lambda (t)
                     (error 'not-found "~a" sym))))))

(define search-path
  (lambda (sym tree succ fail)
    (if (symbol? tree)
        (if (eqv? sym tree)
            (succ (lambda (t) t)) ;succeed with trivial result
            (fail))               ;let fail get another crack
        (if (null? tree)
            (fail)                ;let fail try again
            (search-path sym
                         (car tree)
                         (lambda (path) ;success means taking a car
                           (succ (lambda (t) (path (car t)))))
                         (lambda ()     ;failure means searching more ...
                           (search-path sym
                                        (cdr tree)
                                        (lambda (path) ;... with cdring and ...
                                          (succ (lambda (t) (path (cdr t)))))
                                        fail)))))))    ;... failing up

(let ((t 'a))
  ((search 'a t) t))

(let ((t 'a))
  (search 'b t))

(let ((t '(a b c)))
  (map (lambda (x) ((search x t) t)) t))

(let ((t '(((a b c) d (e (f) g)) h))
      (ss '(a b c d e f g h)))
  (map (lambda (x) ((search x t) t)) ss))

Discussion

I learned this technique from Gregor Kiczales, but it is described in various places, including SICP2, EOPL2, and Friedman's papers. The earlier I've traced it is to deBruijn 1989.


Comments about this recipe

Kanren logical programming system is based on two continuations. http://kanren.sourceforge.net/

-- OlegK - 14 Sep 2004

Contributors

-- ChrisDutchyn - 10 Sep 2004

CookbookForm
TopicType: Recipe
ParentTopic: IdiomRecipes
TopicOrder: 999

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest