PDA

View Full Version : Help with Scheme


YamiShippo
01-03-2005, 06:44 PM
Recently in my computer science class, we pretended that scheme had not implemented numbers and we had to create different functions such as add, subtract, etc with a number system we created which basically comprised of Zs. Examples of these functions are:

Such as 3 = '(z z z)
5 = '(z z z z z)
0 = '()

(define inc (lambda (L)
(cons 'z L)))

(define dec (lambda (L)
(if (null? L)
'()
(cdr L))))

(define add (lambda (a b)
(cond
((null? b) a)
(else (add (inc a) (dec b))))))

(define sub (lambda (a b)
(cond
((null? b) a)
(else (sub (dec a) (dec b))))))

For our project we have to introduce negative numbers:
I) Negative integers.
Introduce a way to represent negative integers, and
write at least six operations that work for both
negatives and positives in your representation
(add, sub, mult, div, greater/less/equals, exp,
fact....)
Our teacher had recommended we indicate the sign of the numbers with either a P for positive or N for negative bfore the Zs. I have been stuck in the way to which i should insert Ps and Ns into the lists.
+3='(P z z z)
-3='(N z z z)
If anyone were to kindly help me start off this project by showing me how to introduce positive and negatives, it would be greatly appreciated. Thank you.