Hacker News new | past | comments | ask | show | jobs | submit login

Fortunately these rules are natural and easy to remember, especially the details of how different languages choose different orders, relative to the confusing and abhorrent prefix notation (- (^ 3 2)) or (^ (- 3) 2).



As a lover of prefix and postfix notation, there is an unambiguous parsing of each of those that does not deepened on any order of operations. Neither lisp nor forth have the question at all - you can only write it exactly as you mean it.

    (- (expt 3 2))
is always -9 without needing to ask which has higher precedence.

    (expt -3 2)
is likewise always 9. There is no question if - is a binary or unary operator in prefix notation and what its order of operation should be.

Likewise, in dc

    3 _ 2 ^ p
    _3 2 ^ p
and

    3 2 ^ _ p
where '_' is the negation operator (it can be used for writing -3 directly as _3, but _ 3 is a parse error) return their results without any question of order of operations.

When you start touching infix, you get into https://en.wikipedia.org/wiki/Shunting_yard_algorithm which was not a fun part of my compiler class.

(And yes, I do recognize your credentials ... I still think that lisp and forth (above examples for dc) are better notational systems for working with computers even if it takes a bit of head wrapping for humans).


Notation is a tool of thought and of communication. The clearest example of infix botching both is probably perl style regex.

Postfix is interesting in forth. It makes the stack manipulations very easy to reason about, and the stack is very important there so this looks like a win. The cost is in coherently expressing complex functions, hence the advice to keep words simple. The forth programmers are doing register allocation interwoven with the domain logic.

Lisp makes semantics very easy to write down and obfuscates the memory management implied. No thought goes on register allocation but neither can you easily talk about it.

Discarding the lever of syntactic representation is helpful for communication and obstructive to cognition. See also macros.


Maybe something's wrong with my terminal. In dc:

    3 _ 2 ^ p   gives 0

    _3 2 ^ p    gives 9

    3 2 ^ _ p   gives 0

    5 _ p       gives 0

    _5 p        gives -5
You didn't intend that I should get those zeros, right?


     ~ % dc -v
     dc 6.5.0
     Copyright (c) 2018-2023 Gavin D. Howard and contributors
     Report bugs at: https://git.gavinhoward.com/gavin/bc

     This is free software with ABSOLUTELY NO WARRANTY.
     ~ % dc   
     3 _ 2 ^ p
     9
     _3 2 ^ p
     9
     3 2 ^ _ p
     -9
     5 _ p
     -5
     _5 p
     -5
     (control-D)
The version that I have appears to have _ parsed as an operator in addition to the negation of a numeric constant.


I am the author of that dc.

You are correct about its behavior.

See https://git.gavinhoward.com/gavin/bc/src/branch/master/manua... (scroll down to the underscore command).


I'm using the version from Debian testing:

    dc -V
    dc (GNU bc 1.07.1) 1.4.1
In fact, in my version "-v" as opposed to "-V" isn't recognized as a valid option.

    3 _ 2 ^ p
    0


I am Gavin Howard, the author of the other dc.

My dc does have a few differences from the GNU dc. I added the extension of using _ as a negative sign.

That is why you are both seeing behavior differences.




Consider applying for YC's W25 batch! Applications are open till Nov 12.

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

Search: