4 ms·
Surely (defmacro double (x) (let ((temp (gensym)) `(let ((,temp ,x)) (+ ,temp ,temp))))
by weavie 5y ago
Surely
(defmacro double (x)
(let ((temp (gensym))
`(let ((,temp ,x))
(+ ,temp ,temp))))
- simiones 5y agoOops, yes...
- exdsq 5y agoAnd this thread is why types help :)
- simiones 5y agoActually, if I had compiled the code produced by macroexpanding the macro, I would have gotten a warning: (compile nil '(lambda (x) (double x))) ;Compiler warnings : ; In an anonymous lambda form: Undeclared free variable TEMP ; In an anonymous lambda form: Unused lexical variable #:G520 Unfortunately, HN doesn't compile code you add in the comments... Note, this is a warning and not an error because of CL's semantics, as the following is a valid program: (let ((temp 100)) (double 10)) ;returns 200 If you really wanted this effect though, normally you would have to declare that `temp` is a special variable: (declaim (special temp)) (compile 'nil (lambda (x) (double x)) ;Compiler warnings : ; In an anonymous lambda form: Unused lexical variable #:G524
- kazinator 5y agoThe correct Common Lisp "macro" is (declaim (inline double)) (defun double (x) (+ x x))