Last active
January 3, 2016 02:39
-
-
Save ha2ne2/8396843 to your computer and use it in GitHub Desktop.
アナフォリックマクロです
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
(require 'cl) | |
(defmacro aif (test then &optional else) | |
(declare (indent 2)) | |
`(let ((it ,test)) | |
(if it | |
,then | |
,else))) | |
(defmacro acond (&rest clauses) | |
(labels ((rec (clauses) | |
(if clauses | |
`(aif ,(first (first clauses)) | |
(progn ,@(rest (first clauses))) | |
,(rec (rest clauses))) | |
nil))) | |
(rec clauses))) | |
;;;; 使用例 | |
;; ハッシュテーブルにkey:takahashi value:35が登録してある状況で | |
(setf *hash* (make-hash-table)) | |
(setf (gethash 'takahashi *hash*) 35) | |
(cond ((gethash 'yamada *hash*) (format "yamada's value was found: %s" (gethash 'yamada *hash*))) | |
((gethash 'takahashi *hash*) (format "takahashi's value was found: %s" (gethash 'takahashi *hash*)))) | |
;=> "takahashi's value was found: 35" | |
; が | |
(acond ((gethash 'yamada *hash*) (format "yamada's value was found: %s" it)) | |
((gethash 'takahashi *hash*) (format "takahashi's value was found: %s" it))) | |
;=> "takahashi's value was found: 35" | |
; と書けます |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment