球拍参数合同错误

问题描述 投票:1回答:1

我已经开始学习合同,并且有这样的程序:

(define/contract (foldr-map f a xs)
  foldr-map/c
  (define (it a xs ys)
    (if (null? xs)
        (cons ys a)
        (let* [(p (it a (cdr xs) ys))
              (fc (f (car xs) (cdr p)))]
          (cons (cons (car fc) (car p)) (cdr fc)))))

  (it a xs null))

(foldr-map (lambda (x a) (cons a (+ a x))) 0 `(1 2 3))

而且我将flodr-map/c合同定义为:


(define foldr-map/c
  (parametric->/c [x a] (->
                         (-> x a (cons/c a number?))
                         a
                         (listof x)
                         (cons/c (listof a) a))))

但是我看到这样的错误:

foldr-map: broke its own contract
  promised: a
  produced: 3
  in: the 2nd argument of
      the 1st argument of
      (parametric->/c
       (x a)
       (->
        (-> x a (cons/c a number?))
        a
        (listof x)
        (cons/c (listof a) a)))
  contract from: (function foldr-map)
  blaming: (function foldr-map)
   (assuming the contract is correct)

我知道程序可以正常运行,因此合同一定是错误的。该过程使用参数f,它是一个函数。

合同具有两个参数:

  • [x是列表xs]的元素>
  • a是累加器
  • 当我将合同更改为:

(define foldr-map/c
  (parametric->/c [x a] (->
                         (-> x number? (cons/c number? number?))
                         a
                         (listof x)
                         (cons/c (listof a) a))))

我收到这样的错误:

foldr-map: broke its own contract
  promised: number?
  produced: #<a>
  in: the 2nd argument of
      the 1st argument of
      (parametric->/c
       (x a)
       (->
        (-> x number? (cons/c number? number?))
        a
        (listof x)
        (cons/c (listof a) a)))
  contract from: (function foldr-map)
  blaming: (function foldr-map)
   (assuming the contract is correct)

所以这时我迷路了。

我已经开始学习合同,并且我有这样的程序:(定义/合同(folder-map fa xs)foldr-map / c(定义(it xs ys)(if(null?xs)(cons ys a) (让* [[p((cdr ...

functional-programming scheme lisp racket
1个回答
0
投票

我找到了解决方案:

© www.soinside.com 2019 - 2024. All rights reserved.