How would you write this in Lisp without introducing a bunch of parens? `if (0 <= x + m && x + m <= n) {…}`
I’m not a Lisp hater, but there’s a reason people make this criticism. I think most people find the infix version easier to read.
How would you write this in Lisp without introducing a bunch of parens? `if (0 <= x + m && x + m <= n) {…}`
I’m not a Lisp hater, but there’s a reason people make this criticism. I think most people find the infix version easier to read.
I unwittingly chose an example that allowed you to write fewer s-expressions in Lisp… touché.
I've been working on this for TXR Lisp. The next, release 300, will support infix.
The core expression in your example expression will parse as you have written it. Except we have to add ifx:
Though it doesn't work since it's not a complete example with defined variables, we can quote it and expand it to see what the expansion looks like, which answers your question of how you write it one underlying Lisp: The ifx macro deosn't have to be used for every expression. Inside ifx, infix expressions are detected at any nesting depth. You can put it around an entire file: Autodetection of infix add some complexity and overhead to the code walking process that expands macros (not to mention that it's newly introduced) so we wouldn't want to have it globally enabled everywhere, all the time.It's a compromise; infix syntax in the context of Lisp is just something we can support for a specific benefit in specific use case scenarios. It's mainly for our colleagues who find it a barrier not to be able to use infix.
In this particular infix implementation, a full infix expression not contained inside another infix expression is still parenthesized (because it is a compound form, represented as a list). You can see from the following large exmaple that it still looks like LIsp; it is a compromise.
I took an example FFT routine from the book Numerical Recipes in C and transliterated it, to get a feel for what it's like to write a realistic numerical routine that contains imperative programming "warts" like using assignments to initialize variables and such:
It was very easy to transliterate the C code into the above, because of the infix. The remaining outer parentheses are trivial.A smaller example is a quadratic roots calculation, from the test suite. This one has the ifx outside the defun:
sqrt is a function, but treated as a prefix operator with a low precedence so parentheses are not required.