add some math built-in functions: even?, odd?, gcd, lcm

This commit is contained in:
Yuichi Nishiwaki 2013-11-09 16:33:52 +09:00
parent fd3300f9db
commit c211d9ffde
1 changed files with 23 additions and 0 deletions

View File

@ -14,6 +14,29 @@
(define (negative? x)
(< x 0))
(define (odd? n)
(= 0 (floor-remainder n 2)))
(define (even? n)
(= 1 (floor-remainder n 2)))
(define (gcd n m)
(if (negative? n)
(set! n (- n)))
(if (negative? m)
(set! m (- m)))
(if (> n m)
((lambda (tmp)
(set! n m)
(set! m tmp))
n))
(if (zero? n)
m
(gcd (floor-remainder m n) n)))
(define (lcm n m)
(/ (* n m) (gcd n m)))
(define (caar p)
(car (car p)))