2013-09-28 60 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))