Saturday, 28 September 2013

How to convert this Scala code to Scheme?

How to convert this Scala code to Scheme?

In Scala, I have this type Set = Int => Boolean how do I/can I mimic that
in Scheme?
For instance, in Scala, I have
def singletonSet(elem: Int): Set = (x: Int) => (x == elem)
def union(x: Set, y: Set): Set = (z: Int) => (x(z) || y(z))
def forall(s: Set, p: Int => Boolean): Boolean = {
def iter(a: Int): Boolean = {
if (a > bound) true
else if (s(a) && !p(a)) false
else iter(a + 1)
}
iter(-bound)
}
In scheme, this is what I have so far:
(define (singletonSet elem) (lambda (x) (= x elem)))
(define (union x y) (lambda (z) (or x(z) y(z))))
(define bound 1000)
(define -bound 1000)
(define (forall s p)
(local ((define (iter a)
(cond
[(> a bound) true]
[(and s(a) (not (p(a)))) false] -- get error of result not
true or false
[else (iter (+ a 1))])))
(iter -bound)))
My second question is how do I get rid of that error?

No comments:

Post a Comment