Live data from Hacker News

Strangest Programming Language Feature?

stackoverflow.com

21–30 of 66 posts

Re: Strangest Programming Language Feature?

#21
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.

Re: Strangest Programming Language Feature?

#22

Earlier quoted context omitted.

This line in MUMPS is not only valid, it also actually does something potentially useful: s:foo'="" foo(foo)=foo It's actually a bit tricky to explain what it does. Everything in MUMPS is effectively a tree. Each tree has a value in the "root node" and you can set it like this: set foo="hello" You can also put data deeper into the tree: set foo("fizz")="buzz" So if something is passed to you and you want to know if y…

It is amazing that MUMPS will be 50 years old in 2 years -- and it still alive and kicking. An emergency MUMPS project is still my highest billing project (per hour) ever.

What was the rate?

Re: Strangest Programming Language Feature?

#23
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.

There's an answer [1] on the linked SO thread (flip to the first page) but basically, the following two lines are equivalent in C:

a[10] 10[a]

This is because a[10] is equivalent to (a + 10), and 10[a] is equivalent to (10 + a), as the first comment on this answer explains.

[1] http://stackoverflow.com/questions/1995113/strangest-languag...

Re: Strangest Programming Language Feature?

#24
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.

[deleted]

Re: Strangest Programming Language Feature?

#25

Fun fact, this is also the reason why 'i' is commonly used as a name for the loop counter variable.

No, it's the other way around; i, j, ... have a long history in mathematics as indices for matrices, summations, etc., with m, n likewise being traditional for the dimensions of a matrix.

In early FORTRAN, integers were present primarily to be used as array subscripts. The INteger mnemonic doesn't appear in any of the early papers or manuals.

As a side note, on the topic of features that would currently seem ‘strange’, some early languages that were intended to be programmed using teletypewriters rather than FORTRAN's Hollerith cards used half-line motions to write array subscripts as actual subscripts.

Re: Strangest Programming Language Feature?

#26
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.

From the c standard[0]:

> The definition of the subscript operator [] is that E1[E2] is identical to (* ((E1)+(E2))).

Since * (E1 + E2) is commutative, E1[E2] == E2[E1].

[0] http://c0x.coding-guidelines.com/6.5.2.1.html

Re: Strangest Programming Language Feature?

#27
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.

I think the trick is that array[i] and i[array] will always be the same value in C.

It's confusing because array[i] makes sense but i[array] doesn't (how the hell can you use an array as an index on an integer?)

It works because array is a pointer to a memory address and i (or index) is an offset. array[i] will add the index to the memory address and return the value there, where as i[array] will add the memory address to the index. Since array+index == index+array, they point to the same memory and return the same value.

Re: Strangest Programming Language Feature?

#29
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.

  By definition, the subscript operator [] is interpreted in such a
  way that ‘‘E1[E2]’’ is identical to ‘‘*((E1) + (E2))’’.  Because
  of the conversion rules which apply to +, if E1 is an array and
  E2 an integer, then E1[E2] refers to the E2th member of E1.
  Therefore, despite its asymmetric appearance, subscripting is a
  commutative operation.¹
¹ Dennis M Ritchie, C Reference Manual, 1975 http://cm.bell-labs.com/cm/cs/who/dmr/cman.pdf (A revised version of this became Appendix A to K&R.)
Post reply on HN