Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have been learning Clojure and looking at Scheme and CL which introduced me to the world of prefix notation. At first I didn't like it but it is still starting to grow on me. To be honest though, there are still long calculations that are difficult for me to understand but I think that is an issue of me needing more exposure/practice and I'll get it.

But that leads me to the question: Which type of notation do you prefer and why?

share|improve this question
2  
Maybe a duplicate of programmers.stackexchange.com/questions/2497/… –  LennyProgrammers Dec 14 '10 at 17:04
    
@Lenny222 - That question is regarding the usefulness, I'm just asking about preference if given the choice. –  Jetti Dec 14 '10 at 19:06
4  
Discrimination! Where's the love for postfix? ;-) en.wikipedia.org/wiki/Forth_%28programming_language%29 –  sdg Dec 14 '10 at 22:20
add comment

8 Answers

I find infix notation easier to read, simply because I've been working with it ever since kindergarten.

share|improve this answer
4  
+1 for not claiming that infix is more intuitive or natural. –  Larry Coleman Dec 15 '10 at 19:34
add comment

I have no preference. I can work well in either.

Not many programmers realize that prefix notation is actually the norm and infix is the oddball. After all, just about every language uses prefix notation for function/method calls. It's only a relatively small number of arithmetic and logic operators that are infix.

The more you look at our system of mathematical symbols, the more screwy you see they are. Some are infix (+, -, *), some are postfix (x2), some prefix (tan x), some under-fix (ratios and fractions), and some overfix. It's not as simple as PEMDAS.

share|improve this answer
3  
I suppose you wanted to say "Not realize many people that and actually is prefix notation the norm is infix the oddball". Parentheses omitted for clarity :P –  back2dos Dec 14 '10 at 19:02
1  
"After all, just about every language uses prefix notation for function/method calls" Actually method calls are infix in most mainstream languages, i.e. receiver.method(arguments), not method(receiver,arguments). –  sepp2k Dec 14 '10 at 19:24
    
(with (edited the-answer) (of suggestion back2dos)) –  Barry Brown Dec 15 '10 at 8:55
1  
@back2dos: And I suppose you wanted to say "Realize not many programmers and that actually is prefix notation the norm is infix the oddball." Not only is it unfair to bring natural language into a discussion about artificial langauge, but also, to quote Wikipedia, "The overwhelming majority of the world's languages are either SVO or SOV, with a much smaller but still significant portion using VSO word order." So actually prefix is more of an oddball than infix, and even less common than postfix. –  Jon Purdy Dec 15 '10 at 21:38
    
SOV... I've always had an affinity for FORTH. And Japanese. –  Barry Brown Dec 16 '10 at 0:09
show 3 more comments

I like prefix for function-like things, infix for operator-like things, postfix for property-like things, and mixfix for control-flow like things:

// prefix:
abs(-3)
sin(PI)

// infix:
1 + 2
4 / 5

// postfix:
list size
employee name

// mixfix:
if a then b else c // (where if/then/else is the mixfix function)

What I don't like is languages that don't let me express things the way that reads most naturally to me. I don't like:

// lisp, have to do prefix for everything:
(+ 1 2)
(if a b c)
(size list)

// forth, have to do postfix for everything:
1 2 +
a b c if
1 + 2
4 / 5
share|improve this answer
2  
Depending on which Lisp you use, you may not need to use prefix for everything. There are infix macro packages available for Common Lisp. –  Larry Coleman Dec 15 '10 at 19:33
    
That's true in theory, but in practice Lispers seem to always go prefix. –  munificent Dec 20 '10 at 23:55
add comment

Prefix, simply because operator precedence is explicit.

FYI, Longer calculations (really, any chain of operations) in Clojure can be broken out using the -> and ->> macros. e.g.:

(-> (get-some-value)
  inc
  (* 2)
  (max 50))

is the equivalent of:

(max (* (inc (get-some-value)) 2) 50)

While the -> macro prepends prior results to the arguments of the next expression, ->> appends them. This is typically very handy when used with functions that expect collections and seqs:

=> (->> "some string we want to capitalize"
     (partition-by #{\space})
     (mapcat #(cons (Character/toUpperCase (first %)) (rest %)))
     (apply str))
"Some String We Want To Capitalize"

Providing facilities for easily composing simpler operations into compound ones like this is one of Clojure's big advantages. For additional reading, check out fogus' post on the -> macro, and then his later post on the topic of "properly thrushy" thrush combinators in Clojure.

share|improve this answer
    
Function composition, you say... –  Inaimathi Dec 14 '10 at 21:19
add comment

Of those two, prefix (postfix could be used the same way, but the only languages I know of that use that are PS and Forth).

  1. It removes the need for any kind of operator precedence and left/right associativity. Both in the sense that you don't have to think about it, and in the sense that the language implementation is slightly simpler since these forces are pre-resolved.
  2. If applied consistently, the resulting code is very close to a tree, which makes parsing easier or unnecessary (and this allows things like programmatic manipulation of that tree structure).
share|improve this answer
1  
It hardly takes 100 lines to resolve the problems you are describing. Parsing really is not a challenge compared to whatever happens next. Programming language syntax is targeted at programmers, not at parsers. –  back2dos Dec 14 '10 at 17:43
    
@back2dos - Agreed, the fact that it's resolved for the implementation is a minor benefit. Why should I (as the programmer) have to figure out what something like (3 + 6) - 8 × 3 / 24 + 5 actually means? Why not just write (+ (- (+ 3 6) (/ (* 8 3) 24)) 5) and remove all doubt, and need for mnemonics? –  Inaimathi Dec 14 '10 at 18:06
1  
For the same reason many people stick with QWERTY. It's the predominant way to do it. Speaking for myself, I can process the first expression much faster. I can see why prefix-notation is better, but I'd really have to get used to it. –  back2dos Dec 14 '10 at 18:49
    
@back2dos - It's true; there was a non-trivial adjustment period the first time I saw the basic arithmetic operators in CL/Scheme. I think it's worth it for the added consistency and precision. YMMV, as always. –  Inaimathi Dec 14 '10 at 21:15
1  
@SHiNKiROU: However, if you have parens, you can vary the arity, and I think (+ a b c d e) is easier to read and write than, say, + + + + a b c d e. –  David Thornley Dec 15 '10 at 20:46
show 2 more comments

I vastly prefer to have prefix when I'm reading code.

But infix when thinking about code.

share|improve this answer
add comment

Postfix. It is clearly the most commonly used in procedural languages and object oriented languages.

Sequential code is postfix notation:

  do x; do y; do z;

OO code is postfix too:

   a.x.y

Most OO and procedural languages used both these conventions heavily mixed with a bit of infix and maybe some prefix to lighten the cognitive load an increase pattern recognition.

Even functional languages use postfix like format commonly:

   let x = g r in
   let y = f x in
   ..

as a way to make the less comprehensible prefix version:

  let y = f (g r) in

easier to read by factorisation (please imagine a longer more nasty chain of applications:)

share|improve this answer
add comment

It depends,

binary operators in which order is important (asymmetric) benefit from infix notation as it is easier to distinguish which way round the operator will be evaluated e.g.

x.Operation(y); //so x is doing "Operation" to y

compared to

Operation(x,y); //what was the order of arguments again?

symmetric operators this is less of an issue, and indeed can sometimes by expanded to be n-ary operators with pre or post fix notation when associative e.g.

sum(1,2,3,4,5);

for pre-fix vs post-fix I prefer prefix as like yoda, you talk, with post-fix

last of all here is what dijstra had to say on the subject in essence he favoured infix for associative operations

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.