User:Eriksiers/Alternate CLISP exponentiation: Difference between revisions

m
updated lang tag
(created)
 
m (updated lang tag)
 
(2 intermediate revisions by 2 users not shown)
Line 1:
This is a better example of exponent (it doesn't solve for negatives however so I will leave in tact the other example):
{{Novice example|Common Lisp}}
 
<syntaxhighlight lang="lisp">
(defun ^ (a b)
(let ((r 1))
(dotimes (x b r)
(setf r (* r a)))))
</syntaxhighlight>
 
<small>(The above example is by [[User:Ahungry|Ahungry]].)</small>
 
I wrote this while reading the first few chapters of [http://www.gigamonkeys.com/book/ Practical Common Lisp]. (I'm about halfway through chapter 4 right now.) It ''seems'' to work -- at least for me, using [[CLISP]] -- and I ''think'' it might be a better version than the "real" solution provided at [[Exponentiation operator#Common Lisp]], but I'm still way too green to know for sure. (This version can handle negative values of '''b''', whereas that version can't -- I think.)
Line 7 ⟶ 16:
Later, I might update this to handle [[Nth root|b as a float]].
 
<langsyntaxhighlight lang="lisp">; calculates a to the bth power (a^b)
; b must be an integer
; a can be integer or floating-point
Line 30 ⟶ 39:
)
)
)</langsyntaxhighlight>
 
Sample outputs:
1,150

edits