Created
March 29, 2015 12:16
-
-
Save MarkLavrynenko/103feabce9e63ad39d22 to your computer and use it in GitHub Desktop.
Macro
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
;Macro study | |
(defun range-helper(x) | |
(if (= x 0) | |
(list x) | |
(cons x (range-helper (- x 1))) | |
) | |
) | |
(defun range (x) | |
(reverse (range-helper (- x 1))) | |
) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;stupid way | |
(defvar ans nil) | |
(loop for x in (range 10) if (= 0 (mod x 2)) do | |
(setq ans (append ans (list x))) | |
) | |
(print ans) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wise way | |
(defmacro lcomp (expression for var in list conditional condition-test) | |
(let ((result (gensym))) | |
`(let ((,result nil)) | |
(loop for ,var in ,list | |
,conditional | |
,condition-test | |
do (setq ,result (append ,result (list ,expression)))) | |
,result))) | |
(setq ans2 (lcomp (+ (* x x) 10) for x in (range 10) if (= 0 (mod x 2)))) | |
(print ans2) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment