3 ms·
The languages I know typically have a fairly standard way of implementing functions - either values or references to values are passed as arguments and then the
by roenxi 9y ago
The languages I know typically have a fairly standard way of implementing functions - either values or references to values are passed as arguments and then the function does something. Maybe it returns a value. That means a construct that short circuits, such as:
`if is.open(file) && read(file) { ... }`
can't actually be implemented by a function. That is, you can't write a function:
`special_and(is.open(file), read(file), ...)`
Because the `read` is automatically evaluated before the function is called.
This means a programming language with lisp-style macros can very easily implement constructs that behave like `&&` and short circuit, because they are implemented as a macro instead of a function. This opens up new (& fast) ways to control program flow that aren't available to a lot of people. The profound impact is that lisp libraries can tack on new control structures in a way that, say, C can't.
I'm no language designer, so this is basically out there to see if I get corrected.
- phamilton 9y agoYep. Short circuit is a great example. Pretty much either macros or lazy evaluation are the only ways to implement it. You can fake lazy evaluation with a closure in other languages.
- theoh 9y agoFor some technical discussion of the order of evaluation of arguments: https://en.wikipedia.org/wiki/Evaluation_strategy#Non-strict_evaluation https://en.wikipedia.org/wiki/Evaluation_strategy#Non-strict... (Short-circuiting is specifically mentioned as having relevance) Note that even in a language which is generally strict, laziness can be provided selectively for individual values, and vice versa. Haskell's laziness has proven to be a cause of troublesome performance problems, sometimes needing to be solved by "strictness annotations"; the newer language Idris is strict, but offers laziness as a type. http://docs.idris-lang.org/en/latest/faq/faq.html#how-can-i-make-lazy-control-structures http://docs.idris-lang.org/en/latest/faq/faq.html#how-can-i-... EDIT: phamilton beat me to it, maybe the links are still useful.
- sampo 9y agoScala also has lazy values and arguments as an option.
- mehrdadn 9y agoYup. And if that's the kind of thing you enjoy control over, you might like Mathematica :)
- pjc50 9y agoI wonder if this could be retrofitted by adding a 'defer' keyword, just as C# has 'out' and 'ref'.
- ghayes 9y agoElixir has macros which work by accepting AST as data and returning transformed AST structures. It's difficult to work with them, but there are plenty of languages that have first-class macros with more complex syntax. From [0]: defmacro unless(clause, do: expression) do quote do if(!unquote(clause), do: unquote(expression)) end end [0] https://elixir-lang.org/getting-started/meta/macros.html https://elixir-lang.org/getting-started/meta/macros.html
- dragonwriter 9y ago> This means a programming language with lisp-style macros can very easily implement constructs that behave like `&&` and short circuit, because they are implemented as a macro instead of a function. With code blocks as values and syntax for unevaluated arguments, you can do this with normal functions without macros being a special, different thing; Rebol/Red do this, for instance.
- pweissbrod 9y agoTotally agreed on this. Twice I've set out to develop DSls only to discover I've invented lisp again and again