Shortest auto-destructive loop
Python, 16 bytes
The non-interesting 0 division approach:
for x in 1,0:x/x
The first iteration computes 1 / 1
, which works fine. The second iteration tries to compute 0 / 0
, resulting in a ZeroDivisionError
being thrown.
17 bytes (personal favourite)
i=1
while i:del i
Initially, i=1
which is truthy, so the loop is entered.
The first time the loop is run, the variable i
is deleted.
This means that, the second time, i
is no longer a variable and therefore its evaluation fails with NameError: name 'i' is not defined.
Another 15 byte solution would be def _():_()
(newline) _()
, because Python does not optimize tail recursion. However, this violates rule #6.
MATL, 5 1 byte
Idea taken from @MartinEnder's CJam answer
`
Try it online!
` % Do...while loop
% Implicit end. The loop continues if the top of the stack is true.
% After the first iteration, since the stack is empty, the program
% implicitly tries to take some non-existing input, and finishes
% with an error
Old version
2:t"x
Try it online!
2: % Push [1 2]
t % Duplicate
" % For each (i.e. do the following twice)
x % Delete top of the stack. Works the first time. The second it tries to
% implicitly take some non-existing input, and finishes with an error
Jelly, 3 2 bytes
Ṿß
Kills itself by running out of memory. Locally does so after ~100 seconds.
Try it online! (death certificate in Debug drawer)
How it works
Ṿß Main link. Argument: x. Implicit first argument: 0
Ṿ Uneval; yield a string representation of x.
ß Recursively call the main link.
Jelly uses TCO, so the first cycle finishes successfully before entering
the next one.
The first few iterations yield:
'0'
'”0'
'””,”0'
'””,””,”,,””,”0'
'””,””,”,,””,””,”,,””,”,,”,,””,””,”,,””,”0'
'””,””,”,,””,””,”,,””,”,,”,,””,””,”,,””,””,”,,””,”,,”,,””,””,”,,””,”,,”,,””,”,,”,,””,””,”,,””,””,”,,””,”,,”,,””,””,”,,””,”0'
After that, it gets real ugly, real fast.