Re: recursion performance
- From: Pascal Costanza <pc@xxxxxxxxx>
- Date: Wed, 30 Nov 2005 00:20:33 +0100
Ulrich Hobelmann wrote:
zion_zii wrote:
Im just starting to program in lisp so please bear with me. As an exercise, I was supposed to write the + function for 2 integers(symbols). This is what I wrote
(defun our+ (n m) (cond ( (zerop m) n) (t (+ (1+ n) (1- m) ) ) ) )
The suggested answer is as follows.
(defun our-add ( n m) (cond ( (zerop m) n) (t (1+ (our-add n (1- m) ) ) ) ) )
My question is this: Is there any difference in performance between the first and the second? Granted, I want to follow what the author has deemed accurate but I dont want to do so blindly. Thanks in advance.
This is a weird exercise. I don't see how it would teach you anything about Lisp because you can do exactly the same exercise in any programming language that supports recursive calls.
Just keep in mind to separate builtin math from the kind of math you're doing here, i.e. don't cheat using builtin "+".
Both versions cheat because even the second version uses the 1+ and 1- functions. It's no wonder that the OP feels confused because the suggested answer is confusing.
If you don't want to cheat, you can only do this by encoding numbers manually, for example implicitly by the number of atoms in a list, or some such.
Again, this exercise doesn't teach anything about Lisp.
Pascal
-- My website: http://p-cos.net Closer to MOP & ContextL: http://common-lisp.net/project/closer/ .
- Follow-Ups:
- Re: recursion performance
- From: Ulrich Hobelmann
- Re: recursion performance
- From: Alex Mizrahi
- Re: recursion performance
- References:
- recursion performance
- From: zion_zii
- Re: recursion performance
- From: Ulrich Hobelmann
- recursion performance
- Prev by Date: Re: sanity check (coding style)
- Next by Date: Re: listo-to-hash
- Previous by thread: Re: recursion performance
- Next by thread: Re: recursion performance
- Index(es):
Relevant Pages
|