Hi Stefan! Stefan Israelsson Tampe <stefan.ta...@spray.se> writes:
> I've hacked on extension on ice-9/match for making modular matching possible > with a reasonable interface. That sounds like a worthy goal to me. Pattern matching in Scheme appears to be limited in this respect compared to other functional languages (OCaml, Scala, & co. whose pattern matchers are effectively extended by defining new types.) > ;;Example, notice ((<op> A B)) means first result of <op> is stored in A and > the second is in B > (define (<op> X) > (match abstractions ((<op> A B)) > X > (['- <op> <op> . L] (cons (- B A) L)) > (['+ <op> <op> . L] (cons (+ A B) L)) > (['* <op> <op> . L] (cons (* A B) L)) > (['/ <op> <op> . L] (cons (/ B A) L)) > ([(? number? X) . L] (cons X L)) > (_ (cons #f #f)))) > > ;;alternatively one can use the more general but wordy <> notation > (define (<op> X) > (match X > (['- (<> <op> A) (<> <op> B) . L] (cons (- A B) L)) > (['+ (<> <op> A) (<> <op> B) . L] (cons (+ A B) L)) > (['* (<> <op> A) (<> <op> B) . L] (cons (* A B) L)) > (['/ (<> <op> A) (<> <op> B) . L] (cons (/ A B) L)) > ([(? number? X) . L] (cons X L)) > (_ (cons #f #f)))) > > (define (rpn x) (car (<op> (reverse x)))) > > ;;and (rpn '(2 4 * 1 -)) evaluates to 7 > > > So e.g. the protocol for a matcher is that the last argument for a matcher > is the list to match on. The matcher should return a cons cell, if the car > element is false the match fails and else it is the value of the match. the > second argument represent the rest of the list after the match has been > removed. Hmm, sorry, I don’t understand what you mean here. Can you come up with a simpler example? What does ‘<>’ mean? Is there a connection between occurrences of ‘<op>’ in patterns and the fact that the procedure is bound to ‘<op>’? Thanks, Ludo’.