Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

So I can not figure out why my numbers are negative in this function. Also, the input for calculate is supposed to be a list of the same 3 if someone could give me a hand with that as well, it would be much appreciated. Thank you.

calculate takes the first number in the list, then multiplies it by the second number in the list and subtracts the third number from the input list.

((calculate '(8 3 7)) '(4 8 2 9)) should return '(29 41 23 44)

(define (calculateHelper n m o L)
  (if (null? L) empty
      (cons ((calculate n m o) (car L)) 
            (calculateHelper n m o (cdr L)))))

;((calculate 8 3 7) '(4 8 2 9))
(define (calculate n m o)
   (lambda (L)
     (if (list? L) (calculate n m o L)
         (- o (* m (+ n L))))))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
245 views
Welcome To Ask or Share your Answers For Others

1 Answer

Among other things, your subtraction was inverted. This should help:

(define (calculate n m o)
  (lambda (L)
    (map (lambda (e)
           (- (* m (+ n e)) o))
         L)))

then

> ((calculate 8 3 7) '(4 8 2 9))
'(29 41 23 44)

EDIT: to call calculate with a list, you could for example use apply to destructure:

(define (calculate nums)
  (apply (lambda (n m o) 
           (lambda (L)
             (map (lambda (e)
                    (- (* m (+ n e)) o))
                  L)))
         nums))

then

> ((calculate '(8 3 7)) '(4 8 2 9))
'(29 41 23 44)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...