Try this:
(+
(-
(* x x)
(* y y))
a)
Crushing expressions into small spaces is not a virtue in either infix
or prefix.
> I ended up writing a simple function to handle infix notation
>
> (defn infix [arg1 func arg2 & args]
> (let [result (func arg1 arg2)]
> (if (= args nil) result (recur result (first args) (second
> args) (rrest args)))))
>
> using that I find makes the code more readable:
>
> (infix (infix x * x) - (infix y * y) + a)
If you're not going to parse fully arbitrary arithmetic expressions
according to the usual (which may as well be arbitrary) operator
precedence, then it's hardly worth it. And it seems to me that the
syntactic significance of parentheses in infix notation is completely
at odds with their role in prefix / S-Expression notation. Thus it
seems that the two are not really compatible.
Ultimately, the superiority of infix notation obtains only in a very
narrow and restricted subset of expressions and this does not, in the
end, justify its incorporation into a fine, elegant language based on
S-Expressions.
> I was wondering if there is a more elegant way to do this, and if it
> could be added as a standard or contrib function.
I don't think so. I would not mind being shown wrong, but I really don't
think so.
As an aside, I'll mention that I read a lot of papers in the domain of
mathematical logic. There the operators are quantifiers (always
prefix), boolean connectives (unary, nonassociative binary or
associative / commutative binary operators) and named predicates and
functions. The convention in such publications is to use prefix for the
unary operators, infix for the nonassociative and A/C binary operators
with a conventional operator precedence and prefix using fully
parenthsized argument lists for named predicates and functions (with
the usual gratuitous parentheses separating arguments). And I'll tell
you, only the simplest such formulas are readily comprehensible. Using
a KIF- or CLIF-like notation (which would appear to be Lisp-like to
denizens of this list) would immensely improve the comprehensibility of
these publications!
Randall Schulz
snip
> ;; used for order of evaluation table and for valid infix operators
> (def +precedence+
> {'rem 5,
> '* 4,
> '/ 3,
> '+ 2,
> '- 1})
What's the significance of this map being named with a leading and
trailing plus? I haven't encountered that before.
--
R. Mark Volkmann
Object Computing, Inc.
Wow. That takes me back. I remember seeing that code once (I was at an
educational institution that had a full source license for Bell Labs
Unix at the time). If I recall, it also did some funky stuff for memory
management. It would just increment a global pointer when it needed
more memory and if it got a segmentation violation signal when
attempting to access that memory, it would sbrk() some more and return
from the signal, allowing the program to continue with the formerly
erring instruction. It was horribly machine- and compiler-dependent,
obviously.
It was all quite grotesque!
> ...
>
> Tom
Randall Schulz
I don't think this strategy will work, 'cause you're operating behind
the Clojure reader. If you really want to pursue non-S-expression
parsing, then I believe you must also write a lexical analyzer and
operate from the character level.
Prolog, Scala and even Prover9 (a first-order logic theorem prover) have
user-definable operators with user-specified precedence, associativity
and "fixity" (pre-, in- or post-). It's certainly intriguing from the
perspective of creativity and expressivity, but I'm not sure how good
an idea it is to allow such complete and radical redefinition of a
program's language and syntax.
I do, however, still support (and actively desire) Common-Lisp-style
programmable reader macros. I don't accept the language fragmentation
argument, frankly. I would not expect it to be used to change Clojure
itself just to suit personal tastes, but rather to enable the
processing of prefix / S-Expression languages that are not compatible
with Clojure per se (two existing examples, one of key importance to
me, come to mind: CycL and CLIF; both are not readable with Clojure but
could be if the reader had Common-Lisp-like programmable reader
macros).
> ...
Randall Schulz
One thing I'll say is that I can't see myself _ever_ getting behind a
notation where white-space is significant. The so-called "semicolon
inference" in Groovy bugs me endlessly (in real-life programming, not
just 'cause I'm aware it's there). And there have been complaints about
the Scala counterpart on its mailing lists. Wasn't FORTRAN lesson
enough about the mistake of giving syntactic significance to blanks
beyond being simple token separators?
Frankly, I don't see the problem with S-Expressions as we've known them
for so long. I think they're beautiful. All non-trivial programs are
complex and require tool support for their creation and even more for
later comprehension. Once you accept that, then you realize that a
uniform notation like the S-Expression is just something that requires
support from the authoring and analysis tools.
Randall Schulz
That seems self-contradictory.
> As Johan points out above, Haskell has a very
> elegant way of infixing functions eg:
>
> div a b
> can be written as
> a `div` b
>
> And it seems that using a macro which accepts arguments in infix
> notation would allow the code to be more expressive and readable.
Maybe, but only if it's possible, and I don't think it is with
sufficient generality to be worthwhile.
> The formula example Jeff provides is quite readable without requiring
> any additional language features.
>
> A general version which does not check operator precedence would
> provide a way to use infix notation to make the code more readable.
Infix without at least an operator precedence scheme is not going to
make anyone more comfortable, since you'll have to fully parenthesize
everything anyway or else limit it to associative-commutative
functions.
I just think this is a very wrong-headed way to approach a Lisp. The
language is what it is and you should meet it head-on and use it on its
own terms.
If you like syntactic sugar, including programmer-defined infix
operators, check out Scala. It's got syntactic sugar in spades.
Randall Schulz
Am I the only person who thinks this is a dead-end proposal that should
be dropped because our BDFL will simply not consider it?
Rich? Will you let us know if this is something we can move on
from 'cause you're simply not going to do it?
I would personally surely hope that reader macros come ahead of infix
parsing...
Randall Schulz
>
> On Sunday 30 November 2008 13:30, André Thieme wrote:
>> ...
>>
>> Although a standard reader macro for infix syntax would be a nice
>> thing to have in Clojure.
>> ...
>
> Am I the only person who thinks this is a dead-end proposal that
> should
> be dropped because our BDFL will simply not consider it?
>
>
> Rich? Will you let us know if this is something we can move on
> from 'cause you're simply not going to do it?
>
Yes please, move on. These things get proposed perennially for Lisps,
but gain no traction because they are not in fact enhancements. People
are free to write their own macros, but I'm not interested in infix
for Clojure.
Rich