News for hackers. Stuff that matters.
Re:Why Lisp? 3 answers by nnq on Tuesday December 11, 2012 @04:16PM (#4906873)
> you can even make your own syntactic extensions, so if infix math is necessary, then you can have it

...in what Lisp and how can one do this?

Re:Why Lisp? 3 answers by fusiongyro on Tuesday December 11, 2012 @04:41PM (#4907006)
Any Lisp, but it turns out to reveal a bunch of Lisp warts. The most obvious one being that no Lisp out there ships with a built-in macro to do this. The subtle truth, though, is that most people who use Lisp start out wanting this and then either give up before they become proficient enough to implement it, or else get acclimated to prefix math and once they are proficient, they don't care anymore.

If you were to set out to do this, the first thing you'd have to accept is that the macro is going to look a little weird. For instance:

    (infixmath 2 + 3 * 4 * (2 + 1))
You're going to want to put spaces between all the terms, every time, because you'd rather have separate atoms for numbers and functions. The next problem you're going to run into is that Lisp doesn't ship with any context-free grammar support akin to Parsec or Yacc. This is the corollary to the point above, that once Lispers become proficient enough to do this they don't want to anymore. Once you're fully enamored with prefix notation, you never find yourself wishing for the kinds of grammar that necessitate this kind of work. (This has a lot to do with why most Lispers hate loop and format too). In other words, you'll be writing your own recursive descent parser from scratch.

Once you've written your recursive descent parser to handle infix math, you'll face the next challenge: what to do with the parse tree. The simplest thing to do is just evaluate it. If you were really clever you might be building a Lisp prefix expression as you went and you can just let Lisp evaluate that directly with eval. A part of you will wonder if you should instead generate a compiled function, but then you have to capture the variables properly and it will be a lot more work, but it could become more efficient, since the Lisp compiler would have access to that information and could do better things with it...

Anyway, my point is that this is sort of a will-o-the-wisp or mermaid example, because it's intended to capture the imaginations of non-Lispers, but actual Lispers have never actually gotten serious enough about it to make a robust implementation and ship it with the system, because the mindset that values infix math is incompatible with the mindset that produces good Lisp programmers.

Re:Why Lisp? 3 answers by betterunix on Tuesday December 11, 2012 @04:49PM (#4907045)
The macro will not necessarily look weird, nor will you necessarily need to put spaces between the terms; in Common Lisp you have reader macros, and you can make this as simple as using [] to delimit regions of infix notations.

As for why you do not see it done in practice, I would say it is mainly because it is not terribly useful in practice. Infix notation is just what most people are comfortable with, because they learned it in school; it has no real technical advantage, and the need for precedence rules is something of a disadvantage (though not a terribly major issue).

Re:Why Lisp? 3 answers by fusiongyro on Tuesday December 11, 2012 @05:37PM (#4907353)
How many projects do you see promoting reader macros? The only package I ever used that came with one was CL-SQL, and it was really unpleasant to get everything working. Reader macros are another example of a feature that Lispers haul out when discussing the benefits of Lisp but never use in practice, because they're almost always more trouble than they're worth.

The reason you perceive this as not terribly useful in practice is my overall point: once you're good enough with Lisp to develop this, you think in terms of s-exps and don't see the point. The primary technical advantage of infix notation is that the book you're getting the formula from used it, so it's easier to see if you transcribed it correctly. A secondary advantage is that it's what you use in your head and on paper. If the advantages of Lisp syntax outweigh the advantages of a lifetime of schooling and compatibility with the world at large, you are, at last, a Lisper. :)

Re:Why Lisp? 3 answers by betterunix on Tuesday December 11, 2012 @06:03PM (#4907505)
Actually I stopped seeing the point of infix notation long before I started programming in Lisp; infix notation stopped seeming like the cat's meow when I had to actually spend time thinking about how to parse it. I have yet to encounter a good argument for infix notation; all anyone seems to offer is, "Well it's what everyone knows and what we teach in schools!" I am not even sure that infix notation being taught in schools should be pointed to as a positive thing, given that "Please Excuse My Dear Aunt Sally" does nothing to improve anyone's understanding of math (when was the last time you saw PEMDAS written in a proof?). I'm sure there would be endless complaints from parents who were unable to help their children with their arithmetic homework if we suddenly switched to postfix notation in elementary school.

I don't think mindlessly transcribing formulas from textbooks is a strong argument for infix notation, and it is definitely not a technical advantage. I am not entirely sure that infix is what people naturally use internally, either, as it is not unusual to hear people say things like, "subtract 3 from x" or "divide the sum of the numbers by the count" -- those look more like something you would see in Lisp than infix notation to me.

Re:Why Lisp? 3 answers by fusiongyro on Tuesday December 11, 2012 @06:16PM (#4907563)
This is one of those debates where there's no convincing anyone on either side. Convention is the only argument I have or need, and it's perfectly sufficient for me. It's insufficient for you and the rest of the Lisp world. This has been the state of affairs for the last fifty years and it will continue to be the state of affairs until the end of time.
Re:Why Lisp? 3 answers by nnq on Wednesday December 12, 2012 @02:07AM (#4909036)
I hope you mean prefix not postfix. Postfix (or Reverse Polish Notation http://en.wikipedia.org/wiki/Reverse_Polish_notation) is completely "unintuitive" for both mathematicians and laymen, and I wouldn't wanna see it in any school... maybe for speakers of RTL languages like arabic it looks more natural, but... no
Re:Why Lisp? 3 answers by tverlinden on Wednesday December 12, 2012 @11:56PM (#4914271)
Try telling that to a Forth programmer. ;)

But seriously, now. Reverse Polish notation is plenty intuitive as long as you don't get too crazy with what you put on the stack.

For example,

  (7**3 + 9*7) / 2
might be rendered:

  7 3 ** 9 7 * + 2 /
That is, "Take 7 and 3 and exponentiate, take 9 and 7 and multiply, then add the two numbers, take the answer and 2 and divide." Parentheses or extra spacing could easily be used to increase readability (though parentheses are never required in RPN; they're purely aesthetic), if that's too much to swallow:

  (((7 3 **) (9 7 *) +) 2 /)
...But really it's just a matter of what you're used to.

P.S. You'll notice that with parentheses RPN looked remarkably Lisp-like. This isn't a coincidence. The core difference between prefix notation as used by Lisp and RPN is that operators in RPN may only take two arguments (it's more complicated in languages like Forth, but since we're talking about arithmetic here we can safely make this assertion). This is the tradeoff RPN makes: losing the ability to have n number of arguments per operator, for the ability to ditch those parentheses for good.

It should also be clear that one could easily have a Lisp-like syntax similar in appearance to the RPN with parentheses above (though with Lisp's ability to have more than two arguments), but just happening to put operators at the end. Conversely, one could have an RPN where operators are at the front, which is actually the concept behind Polish notation, where RPN stems from: http://en.wikipedia.org/wiki/Polish_notation

On a final note, since RPN is read from left-to-right exclusively, a speaker of an RTL language would actually be worse off. The perceived unintuitiveness is more because English is a subject-verb-object language, whereas RPN is subject-object-verb.

Re:Why Lisp? 3 answers by nnq on Thursday December 13, 2012 @05:09PM (#4918404)
never had experience with Forth or anyth similar, so thanks for clearing.

and the fact that fixed arity for an operator or function makes parentheses unnecessary is cool... never thought of that before :)

Re:Why Lisp? 3 answers by marcosdumay on Tuesday December 11, 2012 @08:14PM (#4908054)
Infix notation is compact. That makes it better for parsing by humans with limited short term memory.
Re:Why Lisp? 3 answers by nnq on Wednesday December 12, 2012 @01:55AM (#4909008)
I think it's more about pushing you mind towards a more imperative and sequential way of thinking than compactness (see my comment above)... but this shouldn't be "anti-Lisp" since Lisps have always tried to be "multi-paradigm"...
Re:Why Lisp? 3 answers by fusiongyro on Wednesday December 12, 2012 @02:32AM (#4909089)
For what it's worth, I'm not trying to be anti-Lisp with my remarks. I'm just trying to point out that this particular widely-cited example of a macro is false advertising.
Re:Why Lisp? 3 answers by lispm on Tuesday December 11, 2012 @05:51PM (#4907449)
Reader macros are there to extend s-expressions. Lisp developers who think in s-expressions use reader macros to implement new data structure syntax (sets, tables, JSON, ...), new code syntax (infix, SQL, Objective-C, ...), ...
Re:Why Lisp? 3 answers by nnq on Wednesday December 12, 2012 @01:52AM (#4909002)
I know that infix notations tends to push your intuition towards an imperative and sequential way of thinking somehow. Like, for an expression like:

    ( ...big thing 1... ) + ( ...big thing 2...)

...people tend to think like "compute 'big thing 1' and then, to this, add 'big thing 2'".

...but then again, "purer" languages like Haskell don't use prefix (yes, it's an alternative, but people just don't use it). And mathematical language, that is not imperative and sometimes not even sequential, uses infix. And List never made a fuss about being a "pure" functional language... it's more of a "multi paradingm" language. Still like Haskell's way of putting the operator on parentheses for prefix in infix by default, though H will definitely never be my favorite language...

Re:Why Lisp? 3 answers by fusiongyro on Wednesday December 12, 2012 @02:39AM (#4909111)
I don't see how prefix notation would ameliorate this problem. You'll still wind up with:

    (+ ( ... big thing 1 ...)
       ( ... big thing 2 ...))
I don't think this syntax does a better job of conveying a lack of ordering between the two things. I'm inclined to argue that it's worse simply because Lisp is sequential and the math you did in school wasn't, so you have experience looking at the bigger picture with infix versus prefix. But both of us are bullshitting, because we haven't got any science to support one position or attack the other, only reasonable-seeming hypotheses.

Haskell is my favorite language, but I find I get a lot more use out of converting words to operators than converting operators to "words." For example, I find this:

    x `elem` y
a lot more intuitive than:

    elem x y
because the infix version suggests the natural English reading, that the set is the second argument and the item is the first, where in the prefix case I have to think about it for a second.
Re:Why Lisp? 3 answers by nnq on Wednesday December 12, 2012 @03:18AM (#4909183)
...true, it's bullshitting ...or it's an entirely subjective thing, depending on how you're used to think :)

...but it's that different feeling that "add 'big thing 1' with 'big thing 2'" vs. "compute 'big thing 1' and then add 'big thing 2' to it" ...and if you think of a programming language as an "interface" between your mind and the computer, and think of a programming language from a UI or UX design perspective, these subjective "how it feels" things really do matter. But indeed, Haskell wins on this one 100% by allowing you to just use whatever notation you like without having to write or use a reader macro or something like this. Anyway, back to real work now...

Re:Why Lisp? 3 answers by fusiongyro on Wednesday December 12, 2012 @06:40PM (#4913177)
You use too many ellipses. :)
Re:Why Lisp? 3 answers by fogus on Tuesday December 11, 2012 @04:23PM (#4906915)
You could do it in ~15 lines with Clojure: http://www.fogus.me/fun/unfix/infix-src.html

I suspect most Lisps would require about the same.

Re:Why Lisp? 3 answers by muuh-gnu on Tuesday December 11, 2012 @06:13PM (#4907550)
CL-USER> (ql:quickload "infix")

("infix")

CL-USER> #I(abs(-4)+5^^2-6)

-23

Re:Why Lisp? 3 answers by serichsen on Tuesday December 11, 2012 @04:22PM (#4906913)
Define a function that transforms an infix form to a prefix form. Write a reader macro that understands some special character to mean that the following form is an infix form and uses the function above to transform it. Done.
Re:Why Lisp? 3 answers by betterunix on Tuesday December 11, 2012 @04:22PM (#4906906)
In pretty much any Lisp by using macros. It is a 10 minute exercise in writing a simple parser, or you can just use one of the parser generators out there (plenty for Common Lisp, probably plenty for other Lisps).