scheme macro vs common lisp macro

來源:互聯網
上載者:User

標籤:使用   ar   工作   amp   ad   ef   python   on   

common lisp的let宏定義如下

(defmacro our-left (binds &body body)  `((lambda ,(mapcar #‘(lambda (x) (if (consp x) (car x) x))     binds)      ,@body)      ,@(mapcar #‘(lambda (x)    (if (consp x) (cadr x) nil))binds)));;;;;;;;;;;;;;;;;;;;;test;;;;(our-left ((x 1) (y 2))    (+ x y))展開之後變成=>((lambda (x y) (+ x y) 1 2)這裡我們不難看出其實let不過是lambda的文法糖:)


scheme的let宏定義如下

(defien-syntax let  (syntax-rules ()    ((_ ((x v) ...) e1 e2 ...)     ((lambda (x ...) e1 e2 ...) v ...))))scheme的比上面的那個優雅,直觀吧,我們能不費力氣的看出這個let內部是如何工作的。

 scheme的let*宏如下

(define-syntax my-let*  (syntax-rules ()    ((_ ((p v)) b ...)     (let ((p v)) b ...))    ((_ ((p1 v1) (p2 v2) ...) b ...)     (let ((p1 v1))       (my-let* ((p2 v2) ...)b ...)))))其實scheme的宏是一種模式比對,所以事先一定要把所有的情況的考慮清楚。

那一堆的使用@來解構真的是沒有必要的,看看shceme是如何處理的,根本沒有必要先包裹然後解構啊。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.