Arc Forumnew | comments | leaders | submitlogin
1 point by globalrev 6056 days ago | link | parent

ah solved it now by reversing the variables. but lets say you have a function where you dont know before hand, how would you solve that? edit: changed variablename too.

  (def power (nbr pow)
    (if (is pow 0)
        1
        (if (> pow 0)
	    (let acc nbr
    		(for x 1 (- pow 1)
        		(= acc (* acc nbr))) acc)
	    (let acc 1
    		(for x pow -1
			(= acc (/ acc nbr))) (/ acc 1.0)))))


1 point by tokipin 6056 days ago | link

take a look at http://arcfn.com/doc/iteration.html for a list of different iteration functions. since you aren't using the x variable in those for loops there, you could just use repeat:

  (repeat (abs pow) ... )
here's my version:

  (def power (nbr pow)
     (withs (acc 1.0
             power+ (fn (times)
                        (repeat times
                            (zap * acc nbr))
                        acc))
            (if
              (> pow 0)
                    (power+ pow)
              (< pow 0)
                    (/ 1 (power+ (- pow)))
              1)))

-----