実は match, match-lambda 辺りはよくわかってなかったので良い機会です。
- Twitter / SaitoAtsushi: @valvallow match-lambda* を ...
- Twitter / SaitoAtsushi: @valvallow (define-syntax ...
書いてみました。以下コード。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(use util.match) | |
;; expand image | |
(define fact | |
(match-lambda* | |
((n) | |
(fact n 1)) | |
((n acc) | |
(if (zero? n) | |
acc | |
(fact (- n 1)(* n acc)))))) | |
(fact 10) | |
;; 3628800 | |
;; macro | |
(define-syntax define-overload | |
(syntax-rules () | |
((_ name ((arg ...) body ...) ...) | |
(define name | |
(match-lambda* | |
((arg ...) body ...) ...))))) | |
(define-overload fact | |
((n) | |
(fact n 1)) | |
((n acc) | |
(if (zero? n) | |
acc | |
(fact (- n 1)(* n acc))))) | |
(fact 5) | |
;; 120 | |
(macroexpand '(define-overload fact | |
((n) | |
(fact n 1)) | |
((n acc) | |
(if (zero? n) | |
acc | |
(fact (- n 1)(* n acc)))))) | |
;; (#<identifier user#define> fact | |
;; (#<identifier user#match-lambda*> | |
;; ((n) | |
;; (fact n 1)) | |
;; ((n acc) | |
;; (if (zero? n) | |
;; acc | |
;; (fact (- n 1) (* n acc)))))) |
というかもうマクロにする必要もなさそうですね。。match-lambda* 使えば良いですね・・・。
0 件のコメント:
コメントを投稿