Home > Software design >  How can i add 2 to only even numers using for/list? Racket
How can i add 2 to only even numers using for/list? Racket

Time:04-19

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

CodePudding user response:

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)
  • Related