APL (5 or 6)
If I'm only allowed to initialize the variable to 0, it's 6 characters:
A←0 ⍝ free
⊃A←⍴⍴A
How it works: ⍴A
is the size of A (which is the empty list the first time around, because 0
is a scalar), so ⍴⍴A
is the size of the size of A (which is [0]
the first time, because a one-dimensional empty list has zero values in one dimension). This is then assigned to A (A←
) and the first element is returned (⊃
).
A
is 0
, ⍴A
is []
, then A
is set to ⍴⍴A
which is [0]
, and the first element is returned (0
).
A
is [0]
, ⍴A
is [1]
, then A
is set to ⍴⍴A
which is [1]
, and the first element is returned (1
).
A
is [1]
, so ⍴A
remains [1]
, so A
is set to ⍴⍴A
which remains [1]
and it returns 1
.
If I'm allowed to initialise the variable to anything I want, I can set it to the empty list and drop one of the ⍴
s to make it 5 characters:
A←⍬ ⍝ free, set to empty list
⊃A←⍴A
4Since the task description in fact states that it should return
true
for every time after the initialfalse
, I'd say that solutions that work only a limited time (even if it's 500+ years) don't count as correct solutions. – Joey – 2012-01-26T21:35:50.183