prograMing: simulating Level
- To: mathgroup at smc.vnet.net
- Subject: [mg9891] prograMing: simulating Level
- From: "Xah" <xah at best.com>
- Date: Sun, 30 Nov 1997 20:16:41 -0500
- Organization: smtp.best.com
- Sender: owner-wri-mathgroup at wolfram.com
I want to write a function ***in Mathematica*** that simulates Level. How to do it? A top level guide will do. (but educated guess are welcome too) Few criterions (sp!) for my solution: * not to use some other build-in function with levelspec to circumvent the level problem. * I'm looking for an algorithm that illustrates the principle. Efficiency is not of concern, but elegance is. * I probably am not interested in solutions that uses MapIndexed. Here's some more background: In Mathematica, there's a function called Level, which returns sub expressions at specified level(s) of a tree (list of lists). How can one write a level function in Scheme? The grammar of Mathematica's Level in pseudo-lisp is roughly this: * (Level expr (list a b)) returns an expression that are subexpressions of expr on levels specified by levelspec (list a b). * The levelspec has the form (list a b), meaning from level a to b inclusive. a and b are integers and can be negative. For example, (Level expr (list 2 4)) means all levels from 2 to 4. (Level expr (list 2 -1)) means all levels from 2 to nodes of the tree. (Level expr (list 5 5)) means just level 5. * Level traverses expressions in depth-first order, so that the subexpressions in the final list are ordered lexicographically by their indices. (btw, Level seems natural in languages that support list of lists. What other languages has level build-in? Does Common Lisp? Haskell? ML? Python?) Thanks for any help. References would be appreciated too. Xah, xah at best.com http://www.best.com/~xah/Wallpaper_dir/c0_WallPaper.html "my #1 author: http://www.physics.wisc.edu/~shalizi/Russell/"