
macro ( ('let x y) )
      ( (λ x . ()) y );

macro ('my-match t ps) (tail(
   (let (uuid term) t)
   (let (uuid matched) ())
   (let (uuid return) ())
   (my-match-pats( (uuid term) (uuid matched) (uuid return) ps ))
   (uuid return)
));
macro ('my-match-pats( term matched return () )) ();
macro ('my-match-pats( term matched return (ps (lhs rhs)) )) (
   (my-match-pats( term matched return ps ))
   (if matched () (
      (set matched True)
      (my-match-destructure( term matched return lhs ))
      (if matched (
         (set return rhs)
      ) ())
   ))
);
macro ('my-match-destructure( term matched return () )) (
   (if matched (
      (if term (
         (set matched ())
      ) ())
   ) ())
);
macro ('my-match-destructure( term matched return (:Literal: l) )) (
   (if matched (
      (if (eq( term l )) () (
         (set matched ())
      ))
   ) ())
);
macro ('my-match-destructure( term matched return (:Variable: v) )) (
   (let v term)
);
macro ('my-match-destructure( term matched return (l r) )) (
   (let (uuid x) term)
   (set term (head (uuid x)))
   (my-match-destructure( term matched return l ))
   (set term (tail (uuid x)))
   (my-match-destructure( term matched return r ))
   (set term (uuid x))
);

main := print-s (my-match (1 (() 2)) (
   ()
   ( 1 2 )
   ( (1 (() x)) (x x))
)); 
