recursion - TXR: writing recursive pattern matching directives -
i'm having trouble understanding how write recursive pattern matching functions in txr. below try define recursive directive recognizing file paths. know in case can represent grammar regular expression ([a-z]+\/)+[a-z]+
, have more complex rule set in mind real code benefit it. causing directive fail when there forward slash?
@(define location)@\ @ (cases)@\ @/[a-z]+/@\ @ (or)@\ @/[a-z]+//@(location)@\ @ (end)@\ @(end) @(repeat) @(cases) @{location (location)} @ (output) @location valid location. @ (end) @(or) @location @ (output) @location not valid location. @ (end) @(end) @(end)
example valid inputs:
this/is/valid this/is/also/valid a/b/c
(of course, you're aware location
matching regular language can munge regex: /[a-z]+(\/[a-z]+)*/
. i'm assuming "recursion hello world" warm-up more complicated.)
the thing cases
uses top bottom, short-circuited evaluation. second case cannot match since first case matches prefix. it's not regex branch operator order of subexpressions doesn't matter.
if swap 2 cases, sample works me.
what works (in particular case) changing cases
some
. some
directive doesn't stop @ first match.
using some
not cure-all sort of cases
ordering problem because need short circuit around case terminate recursion (for instance, avoid left-recursing when condition hit) or avoid degenerate performance (exponential time). i'm reminded of university professor's joke: "you've heard of divide , conquer; multiply , surrender".
some
has property later clauses "see" bindings earlier matching clauses. might interfere being solution cases
-ordering problem. say, later clause might fail match due variable clash. :resolve
feature of some
might helpful in situation or might not.
Comments
Post a Comment