From: TJ
Subject: Newbie needs help
Date: 
Message-ID: <FhXm4.625$pN1.8449@news1.rdc1.nj.home.com>
Hey all. I have this problem where the general case is easier to solve than
the specific.

We have all seen the recursive power function: (defun power (x y)...
where y is the exponent. It is easy to implement a recursive function here
just decrement y and test for zero so  you can bail from the recursive loop.

What if I wanted to write a recursive cube program? how can I get the 3 into
the function and then decrement that value to test for 0 if I cannot send it
as an parameter?  The 3 must come from outside the function

(defun cube (x)....


Everytime I try I get either unbounded variable probs or infinite loops.

Any help?
Thanks.
From: Johan Kullstam
Subject: Re: Newbie needs help
Date: 
Message-ID: <m2900zfx5h.fsf@sophia.axel.nom>
"TJ" <·········@home.com> writes:

> Hey all. I have this problem where the general case is easier to solve than
> the specific.
> 
> We have all seen the recursive power function: (defun power (x y)...
> where y is the exponent. It is easy to implement a recursive function here
> just decrement y and test for zero so  you can bail from the recursive loop.
> 
> What if I wanted to write a recursive cube program? how can I get the 3 into
> the function and then decrement that value to test for 0 if I cannot send it
> as an parameter?

> The 3 must come from outside the function

yes

> (defun cube (x)....

you have several ways of doing this.  none of them do exactly what you
want.  use

1) a wrapper function

(defun cube (x)
   (power x 3))

2) an internal function (labels)

(defun cube (x)
   (labels ((power (x y)
              <insert recursive power definition here>))
     (power x 3)))

this is like the wrapper but power is local to cube.

3) &optional args

(defun cube (x &optional (y 3))
   <insert recursive power definition here>)

(cube x 4) actually raises x to the 4th power and that's just plain
silly.  however, other function may have a more natural &optional
extension.

none of these really satisfy in this recursive cubic computer case,
but in other situations these models may make very good sense.

hope this helps.

-- 
J o h a n  K u l l s t a m
[········@ne.mediaone.net]
Don't Fear the Penguin!