You are looking at historical revision 41611 of this page. It may differ significantly from its current revision.

tree-walkers

Replacement of car, cdr and consorts by one operator, walk, of integer arguments. (walk -1) is car, (walk 1) cdr, (walk k) is (iterate cdr k) and (walk -k) (iterate car (- k)). These operaters can (and will) be composed, so, for example, (walk -1 2) is (o (walk -1) (walk 2)), in other words caddr.

The more arguments you give to walk, the deeper you can dig into a tree, i.e. a nested pseudolist.

If you like, you can interprete the arguments of walk as a path for traverseing the tree. For compatibility with the c*r routines, you interprete the arguments from walk from right to left. It might be more convenient, to do it left to right; walk* does this.

The routines

tree-walkers

[procedure] (tree-walkers [sym])

documentation-procedure; returns the list of exported symbols if called with no symbol, otherwise the signature of that exported symbol sym.

iterate

[procedure] (iterate fn k)
[procedure] (iterate fn k start)

The first version creates a new function, composing fn k times. The second applies this new function to a start value.

walk

[procedure] (walk . ks)

Creates a procedure to dig into a tree argument, by transforming the integer arguments, ks, from right to left into corresponding car's and cdr's to be successively applied to the tree.

Here, car is represented by -1, cdr by +1

walk*

[procedure] (walk* . ks)

The same as walk, but accessing the ks from left to right.

Requirements

none

Example


(import tree-walkers)

(iterate add1 5 1)
;-> 6

(define flat '(0 1 2 3))
  (iterate cdr 2 flat)
  ; -> '(2 3)
  ((walk -1 2) flat)
  ; -> 2
  (caddr flat)
  ; -> 2
  ((walk 2) flat)
  ; -> '(2 3)
  (cddr flat)
  ; -> '(2 3)

(define deep '(0 (1 (2) 3) . 4))
  ((walk* 1 -1 1 -2) deep)
  ; -> 2
  ((walk 2) deep)
  ; -> 4
  )

Last update

Aug 09, 2022

Author

Juergen Lorenz

License

Copyright (c) 2022, Juergen Lorenz
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:


Redistributions of source code must retain the above copyright notice,

this list of conditions and the following disclaimer.


Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
Neither the name of the author nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission. 
  
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Version History

1.0
initial version