by qwertyuiop924 · 10y ago · 0 comments
As for hygene problems, Nim's system has the same issues.
Here's an example of the problem:
This is a simple Nim macro from the tutorial:
macro debug(n: varargs[expr]): stmt =
result = newNimNode(nnkStmtList, n)
for x in n:
result.add(newCall("write", newIdentNode("stdout"), toStrLit(x)))
result.add(newCall("write", newIdentNode("stdout"), newStrLitNode(": ")))
result.add(newCall("writeLine", newIdentNode("stdout"), x))
What the heck is that doing? Now let's compare to my lisp of choice, Chicken Scheme (although this macro would be much the same in any Lisp with imperative macros) (define-syntax debug
(ir-macro-transformer
(lambda (x i c)
(let ((exprs (cdr x)))
(cons 'begin
(map (lambda (expr)
`(begin
(write (quote ,expr))
(display " : ")
(display ,expr)
(display "\n")))
exprs))))))
Now see, that's much clearer. And before you ask, yes, this macro is completely hygenic, because it uses ir-macro-transformer. Don't worry too much about it if you're not familliar with Chicken Scheme. It's not the important part.You can write the same macro with less verbosity:
macro debug(n: varargs[typed]): typed =
result = newNimNode(nnkStmtList, n)
for x in n:
let xRepr = toStrLit(x)
result.add(quote do: writeLine(stdout, `xRepr` & ": " & $`x`)) (defmacro debug-it (&body expressions)
`(progn
,@(mapcar (lambda (expression)
`(format t "~%~a: ~a" ',expression ,expression))
expressions)))
Example: CL-USER 8 > (pprint (macroexpand-1 '(debug-it (+ 1 2) (* 3 4))))
(PROGN
(FORMAT T "~%~a: ~a" '(+ 1 2) (+ 1 2))
(FORMAT T "~%~a: ~a" '(* 3 4) (* 3 4)))
CL-USER 9 > (debug-it (+ 1 2) (* 3 4))
(+ 1 2): 3
(* 3 4): 12
NILThere is one key difference between my code and yours: in mine, the functions, values, and symbols referenced in the macro are automatically renamed, thus guaranteeing no namespacing conflicts. Due to the way the CL package system works (IIRC), CL provides almost the same guarantees, at least in this case. But it is an important semantic difference.
And it is the Common Lisp. Despite how you may feel, Scheme, Clojure, PicoLisp, NewLisp, Racket, Interlisp, LeLisp, EuLisp, and others are as much a Lisp as CL.
> and the splicing unquote is less efficient than cons in this case
Not sure why that should be the case and why it even matters.
> From the perspective of someone who knows Lisp, perhaps.
For what it's worth, as a perpetual novice programmer who knows Lisp a little and Nim not at all, I also found qwertyuiop924 (https://news.ycombinator.com/item?id=12617350 )'s example much clearer. (EDIT: Of course it's totally anecdotal, but I thought that it might be worthwhile to have a data point from someone who is not an expert in either language.)
(defmacro debug-it (&body expressions)
`(progn
,@(mapcar (lambda (expression)
`(format t "~%~a: ~a" ',expression ,expression))
expressions)))
That's Common Lisp. I probably could have made mine similarly clean, but I was too lazy to do so :-).Anyways, hats off to lispm, because he's the one who actually did it.
It seems Nim has a code templating system, which is nice, but I find a bit more confusing and less pleasant than the Lisp equivalent. As I said before, it just feels clumsy, especially compared to Lisp.
template debugImpl(varName, value: typed) =
stdout.write(varName)
stdout.write(": ")
stdout.writeLine(x)
macro debug(n: varargs[expr]): stmt =
result = newNimNode(nnkStmtList, n)
for x in n:
result.add(getAst(debugImpl(toStrLit(x), x)))
Whether the Lisp version is clearer or not is very subjective, and you obviously have a lot more experience with it so of course it's clearer for you. To me, the Nim version is far clearer.Edit: flyx's version is even better!