ruricolist / serapeum

Utilities beyond Alexandria
MIT License
415 stars 41 forks source link

`match-of` with ADT (together with `defunion`). #146

Open jsulmont opened 1 year ago

jsulmont commented 1 year ago

Hi Assuming the following following

(defunion ski
  S
  K
  I
  (E (lhs ski) (rhs ski)))

, the following code

  (defun blah (expr)
  (match-of ski expr
    (K K)
    (I I)
    (S S)
    ((E I x) (interpret x))))

interprets I in the (E I x) clause as a variable rather than the I unit type. Or have I missed something (probably)?

Thank you!

jsulmont commented 1 year ago

Answering my own question (after reading pattern-type) ... the correct way of doing this is:

(defun interpret (expr)
  (match-of ski expr
    ((or S K I)  expr)
    ((E (eql I) x) (interpret x))))

(also helpful to read about trivia...)