Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

>The pivotal idea there was the inverse of a function

I was curious about this piece of unexplained cryptic code, so I did a little investigation to see what's going on. Unfortunately there aren't any revolutionary concepts here, just esoteric notation. I'll explain:

To do that "dismal addition" thing, you need to split a number into digits and build a new number using the largest digit at each position. dismal(123, 321) = 323.

APL gives you an operator to make a number out of a sequence of digits: that inverted T you see in OP's code. The left operand is the base. `inverted_T(10, [1, 2, 3]) = 123`.

It gives you another operator to split a quantity into a hierarchy of units. That's the Tee you see in their second snippet. The left operand is a sequence of radixes. An inch is 2.54 cm, a foot is 12 inches, a yard is 3 feet. To transform 130 cm into ft/yd/in, you'd do: `Tee([3, 12, 2.54], 130) = [1, 1, 3.18]`.

So, OP wanted to use this Tee operator to split a number into digits. The problem is, they don't know beforehand how many digits the number has! If it's 2 digits, they must do `Tee([10, 10], number)`. If it's 3, they must do `Tee([10, 10, 10], number)`. (Because `Tee([10, 10], 123) = [12, 3]`). So in the second snippet they tried to do some juggling to get the number of digits and use it in the Tee function (I guess).

What OP really needs is the inverse function of inverted_T. And wouldn't you know it, APL can give you the inverse of a built-in function or a sufficiently simple user function. How? Maybe an operator? No...

See that operator that looks like a puckered face? That operator applies the function to its left, as many times as the operand to its right, to whatever is to the right of the sideways T. BUT, if the right operand is negative, it applies the inverse of the left operand. Basically, the all-powerful "invert function" operation is hidden as a special case of another operator...

In sum, here's my interpretation of OP's code in pseudocode:

Using:

    encode(base, seq) = <base> inverted_T <seq>
    max(a, b)         = a gamma b
    reduce(fn, seq)   = <fn> slash <seq>
    superapply(fn, times, seq) = <fn> puckered <times> sideways_T <seq>


    let dismal =
      encode(10, reduce(max, superapply(encode(10), -1)))
So,

    dismal([123, 321, 111])
applies the inverse of `encode(10)` one time to each sequence item, giving:

    [[1, 2, 3], [3, 2, 1], [1, 1, 1]]
Reduces using max

    max(max([1, 2, 3], [3, 2, 1]), [1, 1, 1])
giving

    [3, 2, 3]
and encodes it in base 10, giving 323.

So that's it. Nice standard library, awful syntax.

I think that OP's "epiphany" was finding a quirk in this esoteric language to counteract another quirk.

Anyway, having satisfied my curiosity, I'm going to promptly forget everything about this :)



The "Inverted T" is usually referred to as a "bottom" symbol (and the not-inverted T-like symbol as "top").




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

Search: