Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Why do they need infix operators? Most programmers already use prefix notation with function calls. Infix is primarily restricted to mathematical and logical operations, but the prefix (lisp style, taking an arbitrary number of arguments) is pretty clear when written neatly:

  (and cond1
       cond2
       cond3)
(NB: multiple lines like this would really only be used for a lot of conditions or longer expressions.)

We already do something similar with our algol-like languages:

  if((cond1 || cond2) && (cond3 || cond4)
                      && (cond5 || cond6)
                      && (cond7 || cond8))
In lisp, CL at least, would look like:

  (if (and (or cond1 cond2)
           (or cond3 cond4)
           (or cond5 cond6)
           (or cond7 cond8))


But at that point it's no longer the language of the domain. If you're going to support writing mathematics that looks like the mathematics that mathematicians write then you need infix operators, because mathematics is written with infix operators. Similarly for many other domains.


That is incorrect; the notation (op arg1 arg2) isn't the surface syntax that is preferred by some practitioners working in that domain. However, it corresponds 1:1 to its abstract syntax.

There are ways to provide infix independently. That is to say, one person can develop this domain specific syntax, and another can develop or customize an infix engine for it.

In Common Lisp, there is a well known "infix.cl" module that provides mappings like a[b,c] -> (aref a b c), f(x, y) -> (f x y), a + b -> (+ a b) and so on.

This isn't understood as creating a new language as such; it's just a sugar. I think it allows new operators to be added with custom precedence and associativity. Or you can hack it however you want.

I've never seen it used in any production code.

The main purpose it serves is to satisfy people who want to know that it can be done; after that it turns out that they don't actually want it done. They just don't want to work with a language that can't do it.

The only program I'm aware of which actually supports writing mathematics that looks like the mathematics mathematicians write (the actual 2D notation) is Tilton's Algebra: written in Common Lisp.

The various mathematics languages out there fail.

Oh, and not to mention that mathematicians have been trained to work with this:

    $\sin{(\frac{\pi}{2}-\theta)} = \cos{\theta}$
That's not such a bad example; I can still sort of see the trig identity in that if I squint my eyes.


Even still, mathematics isn't entirely infix. And outside computer algebra systems, you'll be hard pressed to find examples of the postfix operations expressed as postfix in programming environments. !, for example. What language allows you to express:

  n P k = n! / (n - k)!
as the above? `n P k` will become something like `nperms n k`, factorial will be moved to prefix as `fact n / fact (n - k)`. We're already diverging from the domain language, there's no reason to consider a more logically consistent framework in this case than one that seems to have arbitrarily moved some things from infix to prefix, postfix almost universally to prefix, and left some infix as infix.


>What language allows you...

SML allows you to have an infix function named "P", and Haskell allows something similar, although you have to quote it with backticks and can't use a capital letter to start a function name...

https://en.wikibooks.org/wiki/Standard_ML_Programming/Expres...

https://wiki.haskell.org/Infix_operator#Using_prefix_functio...


I did mean to mention that, but how about creating postfix operators?


In Prolog you have the ability to create new postfix operators. Of course Prolog being a logic language and not a functional language, along with '!' being used for 'cut' opens up another can of worms.

http://www.swi-prolog.org/pldoc/man?predicate=op/3


You're right as far as you go, but it's not an all-or-nothing thing. Most languages won't let you write mathematics exactly like mathematicians do, but the closer you can get the better. For many cases avoiding infix entirely would be a big cost.

(FWIW Scala allows postfix operators, though you'd have to put the n! in brackets)


> Most languages won't let you write mathematics exactly like mathematicians do, but the closer you can get the better

See Wolfram Mathematica for example.

Also, one of my usual DSL tricks is to allow arbitrary TeX in identifiers, which, combined with the literate programming tricks, allows to write very idiomatic mathematical expressions as a code.


You need infix, but that's not enough.

Mathematics isn't written with infix operators alone. Mathematics is written in a fancy 2d notation with all kinds of operator types: infix, prefix, postfix, around-fix, sub-fix, super-fix.

If you look at actual software for maths - several famous ones were written in Lisp like Macsyma, Reduce and Axiom - they provide more than infix.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: