Skip to content

Instantly share code, notes, and snippets.

@ryanwoodsmall
Last active December 28, 2019 08:06
Show Gist options
  • Save ryanwoodsmall/055edcb550562101c308 to your computer and use it in GitHub Desktop.
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
(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))))))
@ryanwoodsmall
Copy link
Author

tested in kawa and sisc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment