'How can i add 2 to only even numers using for/list? Racket

Using Racket: i have been asked to "Add 2 to the even numbers of the list?"

i am not sure how to implement this in the correct format,did try a number of diffrent ways,i just it check those if,there,are,any add 2 to that value.

    ( define add2-list2
( lambda ( l )
( for/list ([ i (even? l)])  (+ 2 i )
)))


Solution 1:[1]

According to the new requirements, the even? test is in the wrong place, you should use an if condition, like this:

(define add2-list2
   (lambda (l)
      (for/list ([i l])
        (if (even? i) (+ i 2) i))))

For example:

(add2-list2 '(1 2 4 5))
=> '(1 4 6 5)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1