Tuesday, 20 April 2010

Recursivity

Since I just mentioned the topic: here is a nice webpage illustrating the concept of recursivity.

(The only problem is that the author is a little bit too excited about his stupid joke.)

Saturday, 10 April 2010

Self Rep

What is the output of this python program self_rep.py?


def self_rep(a): print a; print 'self_rep('+chr(34)+a+chr(34)+')'
self_rep("def self_rep(a): print a; print 'self_rep('+chr(34)+a+chr(34)+')'")


Try "cat self_rep.py", "cat self_rep.py | python", "cat self_rep.py | python | python", ...

This self repeating program is a nice idea I found in the book "Gödel, Escher, Bach" by D.R. Hofstadter. In general the book deals with how Gödel's incompleteness theorem Escher's drawings and Bach's music are related to the notion conciousness. By self-reference and self-repetition.

Gödel's string in plain language can be formulated as: "This sentence cannot be proven in number theory". It is apparently true (if it would be false it would be possible to prove it which means it would be true, which is a contradiction). Hence there is a true statement which cannot be proven. The other question is how this string can be a part of number theory - for that Gödel introduced his Gödel numbers. Finally, with a construction like the program above, he could have the string point at itself.

To finish this post, here is another program:


def self_inc(i,a): print a; print 'self_inc('+str(i+1)+','+chr(34)+a+chr(34)+')'
self_inc(1,"def self_inc(i,a): print a; print 'self_inc('+str(i+1)+','+chr(34)+a+chr(34)+')'")


Now you can try it again: "cat self_inc.py", "cat self_inc.py | python", "cat self_inc.py | python | python", ...