Skip to content

Instantly share code, notes, and snippets.

@MarkLavrynenko
Created March 29, 2015 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarkLavrynenko/103feabce9e63ad39d22 to your computer and use it in GitHub Desktop.
Save MarkLavrynenko/103feabce9e63ad39d22 to your computer and use it in GitHub Desktop.
Macro
;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