计划 - 预计非空列表
问题描述:
仅供参考,我正在使用DrRacket
编程Scheme
。计划 - 预计非空列表
当我运行该程序,我得到了以下错误消息:
check-expect encountered the following error instead of the expected value,
(list false true true true).
:: first: expects a non-empty list; given: string?
我不知道是什么问题,因为它似乎代码应该运行正常。
;; Signature: mymap: (x -> y) listofX -> listofY
;; Purpose: Consumes a function from X –> Y, and a list of X; returns a list of Y;
;; by applying the function to each item in the list of X.
(define (mymap g alist)
(cond
[(empty? alist) empty]
[else
(cons (g (first alist))
(mymap (rest alist) g))]
)
)
;; define 2 other functions using mymap and write
;; one check-expect for each of these functions
(check-expect (mymap string? (list 1 "ab" "abc" "")) (list false true true true))
(check-expect (mymap sqr (list 1 0 -3 4 -5)) (list 1 0 9 16 25))
(define (C2F c)
(+ (* 9/5 c) 32))
(define (cf* alist)
(mymap alist C2F))
(check-expect (mymap C2F (list 100 0 -40)) (list 212 32 -40))
预先感谢您!
答
你倒在你的递归调用的参数mymap
,使用:的
(mymap g (rest alist)))]
代替
(mymap (rest alist) g))]
@ BBladem83:同样,一旦你意识到真正的问题,回去重读错误消息再次。看看你是否明白错误信息是怎么说的。这将有助于未来,因为你一定会不时重复这些错误。我知道我是。 :P如果您在DrRacket中运行,单击该错误消息应该将光标移至违规行。 – dyoo 2014-12-03 23:43:15