recursionschemer5rs

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

given: 1

arguments...:


Solution

  • 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 (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:

    (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.