3 ms·
That may not be the best example of macros since you can do the same thing in Python: def awhen(func, callback): val = func() if val: c
by jdc 11y ago
That may not be the best example of macros since you can do the same thing in Python:
def awhen(func, callback):
val = func()
if val:
callback(val)
- voltagex_ 11y agoThere's probably also a way to write a decorator that does the same thing.
- d0m 11y agoBy the way, I love Python and use it every day, so I'm not trying to get into a language war. The difference between using a decorator and a lisp macro is that using a decorator is "using the syntax of the language to solve your problem", while a macro is "adding a new syntax". I do think there are way in Python (or in C++) to use meta-programming to tweak the language but it always feel very hacky. I.e. I'd hate to jump into a python code-base that isn't standard, such as using @awhen decorator instead of a normal if ;-) What about you? But in Lisp, there's no difference in the syntax.. and adding missing features to the language instead of repeating the same pattern is the standard way to code.
- mikaelj 11y agoIf we were to name your operator "aif" instead (since there is no "when" in Python), and compare if and aif: ## if val = func() if val: # standard form code(val) # of if ## aif aif(func, code) # not obvious that it does the same thing # and limits you to using a function defined # elsewhere, or Python's crippled one-line lambda. Whereas in Lisp, you'd do: ;; regular when (let (val (func)) (when val ; standard form (func val))) ; of when ;; awhen (awhen (func) ; standard form of (func it) ; when, with added bound value ; possibly other ; code goes here ) Do you see how the two forms of Python look very different, and the two forms of Lisp look the same? If you don't like magic, you could invent an awhen that took an extra parameter that designates the name to bind the result to, e.g. (awhen val test). So. Yes - you can achieve the same thing, as in any programming language that is Turing complete. No - you cannot get the same syntax. And syntax affects readability and familiarity.
- d0m 11y agoI hear you but not really because you have to wrap everything in lambdas. Also, you didn't add a whole new variable in the workflow "it" but passed it as a function. You're right that I could have used a better example, such as `when` or whatnot, but that's the first thing that came to me ;-)