From: Jeff Wood Date: 2005-12-24T04:38:35+09:00 Subject: Re: Why not Python? (No, no, I am not a spy) ------=_Part_12930_27437977.1135366703656 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline heh, darn, bug there ... # yes assumptions about positive integer numbers... # factorial of 0 is undefined ... # complete recursive: def factorial( x ) return 1 if x < 1 x * factorial( x - 1 ) end # tail-recursive: def factorial( x, sum =3D 1 ) return 1 if x < 1 factorial( ( x - 1 ), ( sum * x ) ) end # iterative: def factorial( x ) sum =3D 1 for i in (1..x).to_a sum *=3D i end end On 12/23/05, Jeff Wood wrote: > > ah, but to me that isn't completely iterative.... iterative is using a > loop within a function to make sure it and all functions it calls do not > call back up the tree... > > hence: > > # yes assumptions about positive integer numbers... > # complete recursive: > > def factorial( x ) > return 0 if x < 1 > x * factorial( x - 1 ) > end > > # tail-recursive: > > def factorial( x, sum =3D 1 ) > return 0 if x < 1 > factorial( ( x - 1 ), ( sum * x ) ) > end > > # iterative: > > def factorial( x ) > sum =3D 1 > for i in (1..x).to_a > sum *=3D i > end > end > > ... anyways. > > j. > > > > On 12/23/05, Christian Neukirchen wrote: > > > > Jeff Wood writes: > > > > > ... I believe the question was in regards to implementing factorial i= n > > a > > > tail-recursive manner. > > > > > > I believe the statement was: > > > > > > "I'm a complete newb to lisp, I've been reading Practical Common Lisp > > & On > > > Lisp. I've also been working through "The little schemer" and SICP..= . > > I'm > > > running CMUCL under SLIME & Emacs (v21.4). ( phew ) ... I've written > > an > > > iterative & a normally recursive version of factorial ... Now I'm > > trying to > > > figure out how to implement it in a tail-recursive fashion ... can > > anybody > > > help me write a tail-recursive version ?? I'm not familiar with the > > call > > > pattern yet." ... > > > > I see, the iterative version at > > http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-21.html#%_thm_3.= 9 > > actually is the tail-recursive version. :-) > > > > They could have told you that, of course. > > > > > or something like that ... > > > > > > Jeff Wood > > -- > > Christian Neukirchen http://chneukirchen.org > > > > > > > -- > "Remember. Understand. Believe. Yield! -> http://ruby-lang.org" > > Jeff Wood -- "Remember. Understand. Believe. Yield! -> http://ruby-lang.org" Jeff Wood ------=_Part_12930_27437977.1135366703656--