2014-09-22 31 views
0

我试图遵循this question中提供的解决方案,但它根本无法工作。计划开始学生,功能机构额外部分

从本质上讲,我的功能就像这样:

(define (item-price size normal-addons premium-addons discount) 
    (define price 0) 
    (+ price (* normal-addon-cost normal-addons) (* premium-addon-cost premium-addons) size) 
    (cond 
    .. some conditions here 
    [else price])) 

不过,我遇到了以下错误:

define: expected only one expression for the function body, but found 2 extra parts 

现在,我已经试过包装的函数体在'开始',但是当它运行时声称'开始'没有被定义。我使用初学者学生语言版本反对直接球拍。有关解决方法的任何见解?

+2

请注意:'(+ price ...)'行不符合您的想象。它正在计算一个值,但是这个值会丢失,因为你没有分配它 - “价格”的值不会被更新! – 2014-09-22 19:55:25

回答

2

的问题是一样的:在了所使用的语言,我们不能写一个表达式以上的函数体里面,我们不能用begin收拾不止一个表情,都letlambda(这将允许我们创建本地绑定)被禁止。这是一个很大的限制,但我们可以使用,每次计算价格的辅助功能得到解决:

(define normal-addon-cost 10) ; just an example 
(define premium-addon-cost 100) ; just an example 

(define (price size normal-addons premium-addons) 
    (+ (* normal-addon-cost normal-addons) 
    (* premium-addon-cost premium-addons) 
    size)) 

(define (item-price size normal-addons premium-addons discount) 
    (cond 
    ... some conditions here ... 
    [else (price size normal-addons premium-addons)])) 

另外:如果price只能使用一次,只需在网上,计算它的表达,也没有需要创建一个局部变量或辅助函数。

+2

我希望这不会是这样。好吧,我已经做到了。谢谢您的帮助! – Kade 2014-09-22 20:35:45