开发者

rewriting match-lambda

开发者 https://www.devze.com 2023-02-12 06:54 出处:网络
As an exercise I am trying to define a rule match-rewriter that behaves the same as match-lambda but returns its argument if no match is found.So far I have this:

As an exercise I am trying to define a rule match-rewriter that behaves the same as match-lambda but returns its argument if no match is found. So far I have this:

(define-syntax match-rewriter
  (syntax-rules ()
    ((_ (patt body) ...)
      (λ (x) (match x (patt body) ...)))))

which seems to work perfectly if a match is found.

But, I can't figure out how to return the argument x if no match is found.

I know that match will throw an exception if no match is found. But I can't figure out how to catch it and I would like a simpl开发者_运维知识库er solution if one exists.


If this question is about the Racket's match, then just add a clause that matches anything:

(match x [patt body] ... [_ x])
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号