defmacro in Lisp REPL #6
-
What is the correct way to use But it fails when I try it:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
That's the correct syntax, but the problem is running in the x lisp REPL where the expression is wrapped in an implicit scoped return. You can avoid the implicit scoped wrapping by wrapping it in your own x lisp
> (return (defmacro 2+ (n) `(+ ,n 2)))
> (sum (map 2+ [1 2 3])) You can also paste the expression as-is: (defmacro 2+ (n) `(+ ,n 2)) (sum (map 2+ '(1 2 3))) in any LISP textarea on https://sharpscript.net, e.g: https://sharpscript.net/linq/restriction-operators?lang=lisp It's one of the fundamental building blocks in #Script LISP that's used to implement many of its core constructs, e.g: ; defined by
(setq defmacro
(macro (name args &rest body)
`(progn (setq ,name (macro ,args ,@body))
',name)))
; built-in constructs
(defmacro defun (name args &rest body)
`(progn (setq ,name (lambda ,args ,@body))
',name))
(defmacro if (test then &rest else)
`(cond (,test ,then)
,@(cond (else `((t ,@else))))))
(defmacro when (test &rest body)
`(cond (,test ,@body)))
(defmacro let (args &rest body)
((lambda (vars vals)
(defun vars (x)
(cond (x (cons (if (atom (car x))
(car x)
(caar x))
(vars (cdr x))))))
(defun vals (x)
(cond (x (cons (if (atom (car x))
nil
(cadar x))
(vals (cdr x))))))
`((lambda ,(vars args) ,@body) ,@(vals args)))
nil nil))
(defmacro and (x &rest y)
(if (null y)
x
`(cond (,x (and ,@y)))))
(defmacro or (x &rest y)
(if (null y)
x
`(cond (,x)
((or ,@y))))) |
Beta Was this translation helpful? Give feedback.
That's the correct syntax, but the problem is running in the x lisp REPL where the expression is wrapped in an implicit scoped return.
You can avoid the implicit scoped wrapping by wrapping it in your own
(return)
expression, e.g:You can also paste the expression as-is:
in any LISP textarea on https://sharpscript.net, e.g: https://sharpscript.net/linq/restriction-operators?lang=lisp
It's one of the fundamental building blocks in #Script LISP that's used to implement many of its core constructs, e.g: