2

I have a list of patterns, how can I convert them to fit into the match function?

(use matchable)
(define prop '(and (#t #t) #t))
(define patt '(and (X Y) Z)) ;;here is the pattern example
(match prop [('and (X Y) Z) (list X Y Z)])) ;;(#t #t #t) this works
(match prop [patt (list X Y Z)])) ;;anything matches the pattern???

The last match doesn't work, all my variables are symbols, I'm not sure what they are inside the match expression

https://wiki.call-cc.org/man/3/Pattern%20matching

This is the docs, I don't quite understand it yet so maybe someone can help me with an example for what I am trying to do.

shuji
  • 7,369
  • 7
  • 34
  • 49

1 Answers1

3

Several things here:

  • You link to the documentation for CHICKEN 3, which is very very old (back then match was part of the core system). You're using CHICKEN 4, which I can see from the fact that you do (use matchable) at the top, so the docs may not match (pun intended) the matchable version that you're using.
  • Please consider updating to CHICKEN 5, as CHICKEN 4 is not actively developed.
  • Match is a macro which needs to be able to analyse the pattern at compile-time, which means you cannot pass in a dynamic list (which would happen at run-time). It expands into expressions using car, cdr and so on, based on the pattern which pick apart the input. It can't do that if the pattern is not known at compile-time.

If you really must pass in the pattern dynamically, you could do something like this:

(use matchable)

;; Define patt to be available at the macro expansion level
(define-for-syntax patt '('and (X Y) Z))

;; Make a macro that *expands* to the desired match form
(define-syntax match-patt
  (ir-macro-transformer
     (lambda (e i c)
        `(match ,(cadr e)
           (,(i patt) (list ,(i 'X) ,(i 'Y) ,(i 'Z)))))))

;; Actually call the macro to generate the expression
(define prop '(and (#t #t) #t))
(match-patt prop)

Of course this still can only work if the pattern is known at compile-time, so this doesn't really buy you anything unless you're doing really fancy things like reading the pattern at compile-time from a file or something.

When programming in a Lisp (or Scheme), you must always keep in mind at which level of the expansion process you are programming. Usually there are two levels: compile-time and run-time. Macros expand into code at compile-time, so you can never get a macro to operate on information that's available at run-time.

Of course, you could also generate the match expression using eval. Evaluated code again runs in two phases: macro-expansion occurs first, and running the code happens after that. But because you do this all from within a running program, you can inject expressions that were determined at run-time into eval.

sjamaan
  • 2,282
  • 10
  • 19
  • thank you, I was hoping to make more things runtime because macros are a bit hard to work on, I will try eval thanks – shuji Feb 07 '19 at 18:29