2013-09-28 62 views
1

在斯卡拉,我有这个type Set = Int => Boolean我该如何/我可以模仿在计划?如何将Scala代码转换为Scheme?

例如,在Scala中,我有

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) 
    } 

在方案,这是我到目前为止有:

(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] 
        [else (iter (+ a 1))]))) 
     (iter -bound))) 
    (forall v (lambda (x) (= (modulo x 3) 0))) 

所以,你可以做type Set = Int => Boolean在计划/球拍?

回答

3

在计划中,而不是type Set = Int => Boolean,你根本不需要写任何东西:。 Scala需要它的唯一原因是编写参数和返回类型,在Scheme中你都不能这样做。然而,有Typed Racket,其中添加静态类型到球拍,你会写在哪里

(define-type Set (Integer -> Boolean))