Live data from Hacker News

Strangest Programming Language Feature?

stackoverflow.com

51–60 of 66 posts

Re: Strangest Programming Language Feature?

#51
post #9

: 2 3 ; ... redefines the constant 2 as 3. FORTH. Go figure :-)

In Forth, the following are fairly common definitions in the core:

    0 CONSTANT 0
    1 CONSTANT 1
    -1 CONSTANT -1
In an Algolian language like C++ or Java, if the syntax were legal, these would be:

    const 0 = 0;
    const 1 = 1;
    const -1 = -1;
The reason for this is to do with the interpreter. How the Forth interpreter works is very simple:

1. Read one word, where "word" is literally defined as "a sequence of non-space characters delimited by spaces". Some standard words include DUP 2DROP 1+ - . " and :

2. Attempt to find the word in the dictionary. If found, execute it.

3. If it's not found, attempt to interpret it as a number. If that works, push the number on the stack.

4. If neither 2 nor 3 succeed, emit an error message.

So the reason for defining a constant named "0" is to save time: it's quicker to interpret the word "0" if it's in the dictionary; otherwise you have to search the whole dictionary and then do a text-to-number conversion, which takes too long.

So really it does make sense.

Re: Strangest Programming Language Feature?

#56
post #4

It logically follows from how arrays in C works, so I don't really know if it qualifies for weird/suprising, but the old array[i] / i[array] thing is fun to show to people who haven't seen it before. The most amusingly weird thing I can think of is INTERCAL's COMEFROM: http://en.wikipedia.org/wiki/COMEFROM

Can you explain your array[i] / i[array] reference? I can't find it on Google.

Now obviously, c arrays are just strips of memory allocated to that array, so you can access them using the address of the start of the address and the offset (number of array items to skip). To access them, you sum the address and the offset, then you have the address of the item.

     [~~~~~~~~~~~~~~~~[a~~~~~~~b~~]~~~] Memory
     ^–––––––––––––––––^                Address
                       ^–––––––^        Offset
And now, a[b] is just short hand for the pointer arithmetic going on and you could just use any two ints and access any arbitrary memory address (i assume the compiler enforces that this doesn't happen).

     address[offset] => *(address + offset) => getValueAtAddress(valueIn(address) + valueIn(offset))

    a[b]  => *(a + b) => getValueAtAddress(valueIn(a) + valueIn(b))
    b[a]  => *(b + a) => getValueAtAddress(valueIn(a) + valueIn(b))
Those two are obviously the same so *(a + b) == b[a] == a[b]

Re: Strangest Programming Language Feature?

#59
post #9

: 2 3 ; ... redefines the constant 2 as 3. FORTH. Go figure :-)

Certain ancient FORTRAN compilers would let you do that as well. If the target CPU didn't have a "move immediate" instruction, the compiler would just stash the number into memory. The quickest way to implement it is to just add the number into the symbol table... that way an often-used constant will only need one spot in core. Of course when your whole compiler has to run in 8 Kwords or whatever, syntax checking isn…

I haven't heard of that, and it would be great if you could remember where you found it. It sounds to me like it might be an apocryphal corruption of the following:

Fortran passes arguments by reference. If the compiler uses a constant pool, and (lazily or efficiently, depending on your point of view) passes the constant pool location as an argument, a subroutine could inadvertently modify a constant.

FORTRAN 66 (the first ANSI standard) and FORTRAN 77 forbid passing a constant or expression as an argument that will be modified†‡, thereby blessing this implementation. (Fortran 90 and later are tl;dr.)

http://www.fh-jena.de/~kleine/history/languages/ansi-x3dot9-... §8.4 p26

http://www.fortran.com/fortran/F77_std/rjcnf-15.html#sh-15.9...

Post reply on HN