So is EVAL the way to do metaprogramming in #lisp ?
I've just come across EVAL in the beginners course I'm doing. But they say we won't be using it in this course.
@rzeta0 the way to do metaprogramming in lisp is mostly through macros not eval, what is that beginners course?
@rzeta0 Nice, haven't read it. But you'll see the beautiful lisp metaprogramming once you get into macros :)
I've only ever done metaprogramming in Prolog
where you query the database of known facts (functions?) and iterating over the definition, doing with the elements as you please
@rzeta0 I don't know much about prolog metaprogramming. In lisp a macro receives the read s-expr and you can also do with that mostly as you please.
No. When I see »metaprogramming«, I first think of macros.
@rzeta0 Lisp programmer since .. decades. I've never used EVAL for anything other than maybe implementing a REPL. EVAL is not needed for meta-programming.
@rzeta0 It's a starting point, but most of what people would consider Lisp-style metaprogramming is done by "macros".
There's a couple of different theories as to how these should work, though. The Common Lisp idea is, a macro is a program that transforms programs in a new language into programs in a language that the machine already understands. The (modern) Scheme, though, goes a different way, and has instead defined macros as a sort of template expansion language, which can be somewhat easier to understand out of context, but whose powerfulness density tends to be somewhat ower. (But many Scheme dialects implement Common Lisp style macros, as well, even if they're not directly in the R7RS.)
@rzeta0 as others have said, macros is one way to do meta programming, but EVAL is another…. You can have a function that generates expressions and then EVALs them.
Thanks - this is helpful.
What I take away is that most people use macros (which I haven't learned about yet) but it is possible to as you, use eval on a generated expression.
@rzeta0 to do metaprogramming, it depends on which Lisp you are using.
Often times you can use the “backtick” notation along with the “unquote” (comma). So for example:
(defmacro math (left op right) `(,op ,left ,right))
(math (cos t) + (sin t))
--> (+ (cos t) (sin t)))
Scheme has what they call “hygienic macros,” which allow you to use variables internally to the macro that are guaranteed not to shadow any local variables the programmer may have written, and can provide access to private symbols that are available where the macro is defined but not available to where the macro is used. The Guile documentation on syntax-rules
does a pretty good job of explaining it.
@rzeta0 imho its like in js sayings eval is evil ;D. Like the others already told you in lisp metaprogramming is provided by macros. And macros itself can be seen as like templates for programmcode. There are some pitfals though with variables if you don't do the macro properly but there are ways to circumvent it. I guess in on lisp its described how you can handle those cases and make sure you are not using variables from outside of the macro. But lisp is awesome. It opens a new door imho and you are going to see stuff in other languages where you are like in lisp it would be so much easier and more obvious.
I've been putting off doing a lisp course for years so looking forward to seeing what you describe
@rzeta0 you can also take a look at touretzkys gentle introduction ;). https://www.cs.cmu.edu/~dst/LispBook/
Its great as a start even if you have no prior knowledge of programming
in fact that;s the one I'm doing - just started chapter 4
@rzeta0 the only legitimate use of "eval" in modern Lisps is in the tooling that allows for incremental development (e.g. C-x C-e in various Lisp interaction modes in Emacs)