Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

scheme - DrRacket - why is this number negative?

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

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

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...