guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: match-abs


From: Ludovic Courtès
Subject: Re: match-abs
Date: Tue, 31 Aug 2010 00:55:15 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)

Hi Stefan!

Stefan Israelsson Tampe <address@hidden> 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’.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]