【问题标题】:how to add and multiply in scheme如何在方案中进行加法和乘法
【发布时间】:2010-11-14 14:03:42
【问题描述】:

在将值与变量相乘并将它们分配给 Scheme 中的变量时,我需要帮助。

例如我有..

(define overall 0)
(define part1 0.15)
(define part2 0.20)
(define part3 0.4)
(define usrInput1 0)
(define usrInput2 0)

..

我需要做类似的事情

总体 = usrInput*part1 + usrInput*part2 + part3

在方案中

我知道如何将 2 个变量/标量加在一起,但我在这里卡住了,请您指教...

谢谢。

【问题讨论】:

    标签: scheme multiplication addition


    【解决方案1】:

    删除(define overall 0)。然后将overall 定义为您给出的表达式,除了(全括号)前缀表示法而不是中缀:

    (define overall (+ (* usrInput1 part1) (* usrInput2 part2) part3))
    

    无论操作数是标量、变量还是嵌套表达式,在 scheme 中调用任何函数/运算符的语法都是 (operator-name operand1 operand2 ... operandn)

    【讨论】:

      【解决方案2】:
      (define overall (+ (* usrInput1 part1) (* usrInput1 part2) part3))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-16
        • 1970-01-01
        • 1970-01-01
        • 2022-08-11
        • 2018-06-16
        相关资源
        最近更新 更多