joeyh

code written accidentially

joeyh at

I wrote this line of code:

enumFromThen a b = a : enumFromThen b ((b AMult (ANum 2)) AMinus a)

Then I typed this into ghci:

map (fmtArith mempty) [ANum 2,ANum 4..]

Out spewed an infinite list of shell arithmetic expressions, written only in terms of the provided 2 and 4, which when evaluated by a shell, will yield the even numbers 2,4,8,...

Here's the expression that generates the number 22. They get a lot longer. The expression for 42 is 212 kilobytes long.

$(((((((((((((((((((4 * 2) - 2) * 2) - 4) * 2) - ((4 * 2) - 2)) * 2) - ((((4 * 2) - 2) * 2) - 4)) * 2) - ((((((4 * 2) - 2) * 2) - 4) * 2) - ((4 * 2) - 2))) * 2) - ((((((((4 * 2) - 2) * 2) - 4) * 2) - ((4 * 2) - 2)) * 2) - ((((4 * 2) - 2) * 2) - 4))) * 2) - ((((((((((4 * 2) - 2) * 2) - 4) * 2) - ((4 * 2) - 2)) * 2) - ((((4 * 2) - 2) * 2) - 4)) * 2) - ((((((4 * 2) - 2) * 2) - 4) * 2) - ((4 * 2) - 2)))) * 2) - ((((((((((((4 * 2) - 2) * 2) - 4) * 2) - ((4 * 2) - 2)) * 2) - ((((4 * 2) - 2) * 2) - 4)) * 2) - ((((((4 * 2) - 2) * 2) - 4) * 2) - ((4 * 2) - 2))) * 2) - ((((((((4 * 2) - 2) * 2) - 4) * 2) - ((4 * 2) - 2)) * 2) - ((((4 * 2) - 2) * 2) - 4)))) * 2) - ((((((((((((((4 * 2) - 2) * 2) - 4) * 2) - ((4 * 2) - 2)) * 2) - ((((4 * 2) - 2) * 2) - 4)) * 2) - ((((((4 * 2) - 2) * 2) - 4) * 2) - ((4 * 2) - 2))) * 2) - ((((((((4 * 2) - 2) * 2) - 4) * 2) - ((4 * 2) - 2)) * 2) - ((((4 * 2) - 2) * 2) - 4))) * 2) - ((((((((((4 * 2) - 2) * 2) - 4) * 2) - ((4 * 2) - 2)) * 2) - ((((4 * 2) - 2) * 2) - 4)) * 2) - ((((((4 * 2) - 2) * 2) - 4) * 2) - ((4 * 2) - 2))))))

Ever since I first fired it up and typed [1..] into it, I've always loved generating infinite values in ghci and watching it happily print them out as they stream into being, but this is my favorite one ever.

sazius, Tom Marble, lostson likes this.

is there some sort of practical application for this? even if it is just benchmarking?

Also under absurd math, I need to refind the article that suggested that on certain architectures exp(log(x)+log(y)) is faster than x*y.

Efraim Flashner at 2014-12-29T08:49:07Z

Michael Banck likes this.

This would be a stepping stone to making Arith an instance of Integral. However, to write that instance, I need to somehow implement "integer division truncated toward negative infinity" (ie, -5 / 3 => -2) in shell arithmetic expressions.

Which seems difficult because shell math only supports integers, and it always truncates toward 0 when dividing.

joeyh at 2014-12-29T15:54:00Z

Hmm, I could use modulus, couldn't I? 5 % 3 is 2, which is more than half of 3, so subtract one.

joeyh at 2014-12-29T16:00:03Z