Here is a code for factorial in Python
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
Now code in Lisp
(defun factorial (n)
(if (= n 0)
1
(* n (factorial (- n 1)))))
In Lisp, operators come first in the list (prefix)
so in Python if you say
(2 + 3)it is
(+ 2 3)in Lisp.
Python:
(2 + 3 + 4 + 5)
Lisp:
(+ 2 3 4 5)
Just assume all those paranthesis are not there. You will find it almost like Python
Here is the Lisp version with some paranthesis removed
defun factorial (n)
if (= n 0)
1
* n factorial (- n 1)
How hard is that :)
Long back ago, when i was at the peak of Python craze, i saw Lisp and was quite frustrated by all its (). I then decided that i will not learn this language ever in my life. I was wrong. It seems there are many powerful features which Lisp has, and no other language has. So only way to learn is to learn it :)
Now here is the Python and Lisp version of Tower of Hanoi. Try figure
Python:
def towerOfHanoi (disks, fromp, to, spare):
if disks:
towerOfHanoi (disks[1:], fromp, spare, to)
print "Move %s from %s to %s" % (disks[0], fromp, to)
towerOfHanoi (disks[1:], spare, to, fromp)
towerOfHanoi ([3, 2, 1], 'a', 'b', 'c')
Lisp:
(defun towerOfHanoi (disks from to spare)
(unless (endp disks)
(towerOfHanoi (cdr disks) from spare to)
(format t "Move ~A from ~A to ~A~%" (car disks) from to)
(towerOfHanoi (cdr disks) spare to from)))
(towerOfHanoi '(3 2 1) 'a 'b 'c)
5 comments:
Have you thought about teaching Computer Science?
i am not qualified for that
I think u are qualified
Many ppl with just B.tech teaches in colleges
Ahem! Seems like a new craze - LISP!
Happy Coding ;-)
And say Hi to that LOGIX friend of yours .. lol .. if he is still outsourced himself to bangalore .
>>I think u are qualified
Many ppl with just B.tech teaches in colleges
hmm..
>>And say Hi to that LOGIX friend of yours .. lol .. if he is still outsourced himself to bangalore .
:D neo im very much sorry about that :)
others: one day i took neo along for one python meetup here, there was a talk on logix (www.livelogix.com) .. it was too much .. poor neo was gasping the whole 2 hours :D
Post a Comment