Scheme编程:与List交互时Function中的Contract Violation Error

Scheme Programming: Contract Violation Error in Function when interacting with List

我被指示执行以下操作: ( insertBag List Item ) -- return 表示在给定列表中插入给定项目的结果的新包

;Function Two: insertBag
;@Param: List, Item
;@Return: The new bag that represents the result of
;inserting the given item in the given list.
;Important Note: There are two Helper functions for insertBag
;Helper Function One: newPair
;Helper Function Two: addPair 

(define (insertBag List Item)

 ;Check if we have an Empty List
 (if (null? List)
  (newPair Item)
   
   (if (string=? (car(car List)) Item)
    (cons (addPair (car List)) (cdr List))
    (cons (car List) (insertBag(cdr List) item))
   )
 )
)

;Helper Function One: newPair

(define (newPair Item)
 (cons Item 1)
)

;Helper Function Two: addPair

(define (addPair List)
 (cons (car List) (+ 1 (cdr List)))
)


;Test Run Case for insertBag
(insertBag '(("a".2)("d".1)("c".3)) "a"); Input for A

但是,我收到以下错误:

; +: contract violation
;   expected: number?
;   given: '(0.2)
;   argument position: 2nd
; [,bt for context]

请注意,我被指示不要使用 lambda。我会很感激一些帮助! 谢谢! >

样本输入有问题,你必须在点之间写空格:'("a".2)是一个包含"a"和[=13=的列表] 作为其元素,而 '("a" . 2) 是一个 cons 对,其中 car 部分为 "a"cdr 部分为 2。通过这个小改动,您的代码将按预期工作:

(insertBag '(("a" . 2) ("d" . 1) ("c" . 3)) "a")