球拍参数合同错误
Racket parametric contracts errors
我已经开始学习合约了,我有这样的程序:
(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
。
合约有2个参数:
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)
所以在这一点上我迷路了。
我找到了解决方案:
(define foldr-map/c
(parametric->/c [x a] (->
(-> x a (cons/c a a))
a
(listof x)
(cons/c (listof a) a))))
这是有道理的,而且看起来很明显,但花了相当多的调试时间才弄明白。
我已经开始学习合约了,我有这样的程序:
(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
。
合约有2个参数:
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)
所以在这一点上我迷路了。
我找到了解决方案:
(define foldr-map/c
(parametric->/c [x a] (->
(-> x a (cons/c a a))
a
(listof x)
(cons/c (listof a) a))))
这是有道理的,而且看起来很明显,但花了相当多的调试时间才弄明白。