Functional programming 球拍参数误差
我已经开始学习合同,我有这样的程序:Functional programming 球拍参数误差,functional-programming,scheme,lisp,racket,Functional Programming,Scheme,Lisp,Racket,我已经开始学习合同,我有这样的程序: (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))
(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)
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)
我知道程序是正确的,所以合同肯定是错误的。该过程采用参数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: 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)
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)
foldr地图:违反了自己的合同
承诺:号码?
制作:#
in:的第二个参数
第一个论点
(参数->/c)
(x a)
(->
(->x编号?(cons/c编号?编号?)
A.
(共十个)
(cons/c(列表a)a)
合同来源:(功能文件夹映射)
责备:(功能图)
(假设合同是正确的)
所以在这一点上我迷路了。我找到了一个解决方案:
(define foldr-map/c
(parametric->/c [x a] (->
(-> x a (cons/c a a))
a
(listof x)
(cons/c (listof a) a))))
这是有道理的,而且似乎有点明显,但它花了相当多的调试时间来弄清楚