【发布时间】:2021-04-11 07:45:22
【问题描述】:
我被指示执行以下操作: ( insertBag List Item ) -- 返回一个新包,表示在给定列表中插入给定项目的结果
;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。我会很感激一些帮助! 谢谢! >
【问题讨论】: