‹ BackHN Continuity

Thread

Needed 1+1, built a functional programming language

151 points · 80 comments · birdculture

  1. ReDress · · focus · HN ↗
    I'm not sure whether someone else has commented on this. I am yet to read the comments. Maybe I will read them soon enough.

    Anyways and however, speaking in strict mathetical sense, the model of a tree actually breaks the classical mathematical model of operator precedence.

    1 + 1 + 1 evaluates to:

         (+)
         / \
       (+) (1)
       / \
     (1) (1)
    
    The above will be correct, mathematically, but will break once you involve multiple mathematical operators in the statement. Because, mathematically, operators have precedence.

    The operators are essentially ordered based on their depth into the right of the question / statement but unless I am terribly wrong, this is not the case in mathematics and some operators have higher precedence regardless of their position in the statement.

    Any comments on this?

    1. kccqzy · · focus · HN ↗
      Operators not only have precedence, but also associativity. Building a parser that works for arbitrary precedence and associativity is a standard school project that takes only a few lines more code than a parser that deals with operators with a single precedence level.

      Here is an example in Haskell for operator parsing that uses nothing no more than the standard library: <a href="https:&#x2F;&#x2F;hackage-content.haskell.org&#x2F;package&#x2F;parser-combinators-1.3.1&#x2F;docs&#x2F;Control-Monad-Combinators-Expr.html" rel="nofollow">https:&#x2F;&#x2F;hackage-content.haskell.org&#x2F;package&#x2F;parser-combinato... Excluding comments it’s less than 50 lines of code, and it handles arbitrary precedence, prefix and postfix, ternary operators, infix operators with all three kinds of associativity.

Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.