1

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

那么你可以type Set = Int => Boolean在方案/球拍中做吗?

4

1 回答 1

3

在 Scheme 中,type Set = Int => Boolean您无需编写任何内容:. Scala 需要它的唯一原因是编写参数和返回类型,这在 Scheme 中都做不到。但是,有Typed Racket,它将静态类型添加到 Racket 以及您将在哪里编写

(define-type Set (Integer -> Boolean))
于 2013-09-28T13:15:03.877 回答