如何将此Scala代码转换为Scheme?

时间:2013-09-28 10:11:25

标签: scala scheme racket

在Scala中,我有type Set = Int => Boolean我如何在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)))

你可以在Scheme / Racket中做type Set = Int => Boolean吗?

1 个答案:

答案 0 :(得分:3)

在Scheme中,您需要完全不写type Set = Int => Boolean而不是(define-type Set (Integer -> Boolean)) 。 Scala需要它的唯一原因是编写参数和返回类型,这两种类型都不能在Scheme中完成。但是,有Typed Racket,它将静态类型添加到Racket以及您要编写的位置

{{1}}