方案::R5RS 使用映射递归


;; Write the code to fill in the missing part (???) of the below statement)
;;(map ??? (list 1 2 3 4 5 6 7 8 9 10)) => (2 4 6 16 10 36 14 64 18 100)
;; *2 ^2 *2 ^2
(define (mapp list item)
(cond ((odd? (car item)) (* (car item) 2))
(cons ((even? (car item)) (* (car item) (car item)))
(mapp (list (cdr item))))))
(mapp (list 1 2 3 4 5 6 7 8 9 10))

你能帮我解决这个问题吗?谢谢

错误消息:
预期的参数数与给定数量不匹配

预期:2

给定: 1

参数。。。:

注释代码中的问题与您编写的过程完全不同,它要求您使用map并传递一个将生成序列的lambda,如示例中所示:

(map (lambda (e)
(if (odd? e) (* 2 e) (* e e)))
(list 1 2 3 4 5 6 7 8 9 10))
=> '(2 4 6 16 10 36 14 64 18 100)

如果你想实现mapp- 你自己的map版本,特别是解决这个问题,它会像这样:

(define (mapp lst)
(cond ((null? lst) '())
((odd? (car lst))
(cons (* 2 (car lst)) (mapp (cdr lst))))
(else
(cons (* (car lst) (car lst)) (mapp (cdr lst))))))
(mapp (list 1 2 3 4 5 6 7 8 9 10))
=>'(2 4 6 16 10 36 14 64 18 100)

请注意,您只需要一个参数,即列表。事实上,问题中的原始错误是因为您定义了具有两个参数的过程,但只传递了一个参数。

最新更新