0

作为参考,我正在Scheme使用DrRacket.

当我运行程序时,我收到以下错误消息:

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))

先感谢您!

4

1 回答 1

3

您在递归调用中反转了参数mymap,使用:

      (mymap g (rest alist)))]

代替

      (mymap (rest alist) g))]
于 2014-12-03T20:50:21.290 回答