Live data from Hacker News

Ask HN: What would Your_Favorite_Compiler do?

news.ycombinator.com

1–10 of 28 posts

Ask HN: What would Your_Favorite_Compiler do?

#1
x = 10

y = x + ++x

What is y?

Even the professor who teaches compiler construction at my university thought it was interesting to see how different languages or compilers handle this. I've tried a couple but leave it to you to post and discuss your results.

(I know it's terrible code)

Re: Ask HN: What would Your_Favorite_Compiler do?

#2

  (let* ((x 10)
         (y x))
    (+ x (incf x)))

  vs

  (let* ((x 10)
         (y x))
    (prog1
      (+ x x)
      (incf x)))   

  [INCF does it with side-effects, use 1+ or directly evaluate  (+ 1 x) for the clean version]
Not everyone puts up with semantic ambiguity in operator precedence and evaluation. Some of us program in parse trees, directly :-)

Re: Ask HN: What would Your_Favorite_Compiler do?

#5
My favourite compiler, would have a manual one page long. I should be able to read it and program 'hello world' in five minutes. It should come with about 40 primitives and leave the rest to me and its community!(It should also throw an error if you type y=x + ++x

:)

Re: Ask HN: What would Your_Favorite_Compiler do?

#6
post #2

(let* ((x 10) (y x)) (+ x (incf x))) vs (let* ((x 10) (y x)) (prog1 (+ x x) (incf x))) [INCF does it with side-effects, use 1+ or directly evaluate (+ 1 x) for the clean version] Not everyone puts up with semantic ambiguity in operator precedence and evaluation. Some of us program in parse trees, directly :-)

yes, "operator precedence" is of course the key

Re: Ask HN: What would Your_Favorite_Compiler do?

#9
Think about how this is compiled. In machine language there are no nested expressions, so the compiler will have to split the expression up.

    expression( ... ++x ... )
will probably be translated to

    x = x+1;
    expression( ... x ... )
This is the simplest way to compile ++x:

    1. put x=x+1 before the expression
    2. replace ++x with x
So I think most languages would get 22.

But in languages where + is a function there's a good chance you get 21.

Re: Ask HN: What would Your_Favorite_Compiler do?

#10

Interpreter, really: >>> x = 10 >>> x + ++x 20 I suspect my other favorite compiler will say 'Type error: Could not match "Num a => a" against "Num a => [a] -> [a]"', but I don't want to wait for it to finish installing.

yup, that's python alright. Very odd behaviour
Post reply on HN