4 ms·
Show us some new control structures using your, um, scheme.
by logjam 18y ago
Show us some new control structures using your, um, scheme.
- JesseAldridge 18y agoI'm not exactly sure what you mean by "control structures", but how about this: def switch(case, actions): macro(actions[case]) switch(2, ["print 'foo'", "print 'bar'", "print 'baz'"])
- silentbicycle 18y agoFor one example, what about if the arguments in the switch list had side effects? >>> def boo(x): ... print "BOO" ... return x + 3 >>> [1, 2, 3, boo(4) + 0, 5][1] will result in BOO 2 . Each element of the list would be evaluated down to a result value, then the appropriate one is chosen. I'm pretty sure you would need to wrap every item in the switch list in a function just to delay the evaluation. This is just as true for if a different Python interpreter were executing generated code. Adding control structures means extending the core language, such as being able to add a new exception-catching construct that could return evaluation to where it was thrown if, after passing through the exception handler, the preceding test no longer holds true. Or a command that backs up the surrounding for loop by one step. For example. What you wrote above would be useful for some things (generating boilerplate code, certainly), but it doesn't alter the underlying language.
- BrandonM 18y agoThat's why each of the arguments is supposed to be a string of Python code, instead of raw Python code. That's also why the Python "solution" is much uglier than Lisp macros.