DrRacket - 为什么这个数字是负数? [英] DrRacket - why is this number negative?

查看:60
本文介绍了DrRacket - 为什么这个数字是负数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我不明白为什么我的数字在这个函数中是负数.此外,如果有人也可以帮我一下,计算的输入应该是相同的 3 的列表,我将不胜感激.谢谢.

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 取列表中的第一个数字,然后将其乘以列表中的第二个数字,并从输入列表中减去第三个数字.

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)) 应该返回 '(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))))))

推荐答案

除此之外,你的减法被倒置了.这应该会有所帮助:

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

然后

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

<小时>

EDIT:使用列表调用 calculate,例如您可以使用 apply 来解构:


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

然后

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

这篇关于DrRacket - 为什么这个数字是负数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆