Scheme - 期待非空列表

时间:2014-12-03 20:48:33

标签: list scheme racket empty-list

作为参考,我使用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))

提前谢谢!

1 个答案:

答案 0 :(得分:3)

您将递归调用中的参数反转为mymap,请使用:

      (mymap g (rest alist)))]

而不是

      (mymap (rest alist) g))]