简体   繁体   English

SCHEME::R5RS 递归使用地图

[英]SCHEME::R5RS Recursive using map

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

Can you help me solving this problem?你能帮我解决这个问题吗? Thank you谢谢

error msg:错误消息:
the expected number of arguments does not match the given number预期的参数数量与给定的数量不匹配

expected: 2预期:2

given: 1给定:1

arguments...:参数...:

The question in the commented code is completely different to the procedure that you wrote, it's asking you to use map and pass a lambda that will produce a sequence as in the example:注释代码中的问题与您编写的过程完全不同,它要求您使用map并传递一个lambda ,该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)

If you wanted to implement mapp - your own version of map that solves this problem in particular, it'd go like this:如果你想实现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)

Notice that you only needed one parameter, the list.请注意,您只需要一个参数,即列表。 In fact, the original error in the question was because you defined a procedure with two parameters, but you were passing only one.事实上,问题中的原始错误是因为您定义了一个带有两个参数的过程,但您只传递了一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM