Addendum :
An exact number extention
-----------------------------------
(define (fr# a b) (list a b))
(define (simplef a)
(let* ((p (car a)) (q (cadr a)) (s (gcd p q)))
(list (/ p s) (/ q s))))
(define (fneg a)
(list (* -1 (car a)) (cadr a)))
(define (finv a)
(list (cadr a) (car a)))
(define (f+ a b)
(let ((p (car a)) (q (cadr a)) (r (car b)) (s (cadr b)))
(simplef (list (+ (* p s) (* r q)) (* q s)))))
(define (f- a b)
(f+ a (fneg b)))
(define (f* a b)
(let ((p (car a)) (q (cadr a)) (r (car b)) (s (cadr b)))
(simplef (list (* p r) (* q s)))))
(define (f/ a b)
(f* a (finv b)))
(define (fprint a)
(display (car a)) (display "/") (display (cadr a)))
---------------------------------------------------
definitions in action :
TinyScheme 1.39
> (define a (fr# 1 3))
a
> (fprint (f+ a a))
2/3 #t
> (fprint (f- a a))
0/1 #t
> (fprint (f* a a))
1/9 #t
> (fprint (f/ a a))
1/1 #t
> (define b (fr# 3 4))
b
> (fprint (f* a b))
1/4 #t
(remark I coded it to simplify the fraction if possible )> (fprint (f/ b a))
9/4 #t
> (fprint (f/ a b))
4/9 #t
>
------------------ to do : the div zero exception (for conversion only , except for the simplifaction I avoided division and the gcd should never be 0 - but i have to test this ) , (fraction->inexact )------------- some cosmetics
such things are exactly the power of Lisp -- Mike of course write it for BL too (just need to define (gcd ) for you
(now denominator etc ... can be defined)
best Rob
extending further
(define (make-harmonic x)
(let ((m '() ))
(do ((i 1 (+ i 1)))
((= i x))
(set! m (cons (fr# 1 i) m))) (reverse m)))
(define (print-harmonic L)
(map (lambda (x) (fprint x) (display " + ")) L)
(display " ...."))
(define (calc-harmonic L fr )
(if (null? L) fr
(calc-harmonic (cdr L) (f+ fr (car L)))))
--------------in action -----------
> (define h (make-harmonic 20))
h
> (print-harmonic h)
1/1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7 + 1/8 + 1/9 + 1/10 + 1/11 + 1/12 + 1/13
+ 1/14 + 1/15 + 1/16 + 1/17 + 1/18 + 1/19 + ....#t
> (calc-harmonic h (fr# 0 1))
(275295799 77597520)
> (fprint (calc-harmonic h (fr# 0 1) ))
275295799
/77597520 #t
>
Joh, ; there's also
SLIB , but do not know any one connected it with TinyScheme (for Bigloo this extension can be linked)