Re: Factor

Factor: the language, the theory, and the practice.

2 + 2 = 5

Thursday, June 5, 2014

#math #syntax

There is an old programmer joke that wonders if 2 + 2 = 5 for very large values of 2 (someone even made it into a fun T-shirt).

Well, a challenge on StackExchange to write a program that makes 2 + 2 = 5 caught my eye. I wondered what a solution might look like in Factor.

If you run this code:

IN: scratchpad << "\x32" create-in 5/2 define-constant >

Then… whoa!

IN: scratchpad 2 2 + .

As it turns out, a little bit ago I noticed that you can redefine numbers this way and filed a bug to start a conversation about this “feature”.

This exploits the parser, particularly the parse-datum word which searches a token for an already defined word, then if not found, tries to parse it as a number. Usually, we disallow words from being defined by a number using scan-word-name, but that doesn’t prevent you from doing it yourself as in the example above.

P.S., in the spirit of the Haskell solution (and anyone else that craves infix notation):


IN: scratchpad 2+2 .