Last active
December 28, 2019 08:06
-
-
Save ryanwoodsmall/055edcb550562101c308 to your computer and use it in GitHub Desktop.
quick and dirty "x ^ y" power function in scheme to raise a base to an exponent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (myabs x) | |
(cond ((< x 0) (* x -1)) | |
((x >= 0) (* x 1)))) | |
(define (myexpt x y) | |
(cond ((< x 0) (* (cond ((odd? (myabs y)) -1) | |
(else 1)) | |
(myexpt (myabs x) y))) | |
((< y 0) (/ 1 (myexpt x (myabs y)))) | |
((= y 0) 1) | |
((> y 0) (* x (myexpt x (- y 1)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
tested in kawa and sisc