[racket] Basic Racket Question

From: Sayth Renshaw (flebber.crue at gmail.com)
Date: Tue Dec 21 21:49:41 EST 2010

On Wed, Dec 22, 2010 at 1:19 PM, Sayth Renshaw <flebber.crue at gmail.com>wrote:

>
>
> On Wed, Dec 22, 2010 at 1:02 PM, Danny Yoo <dyoo at cs.wpi.edu> wrote:
>
>> >>  (define (netpay gross tax-rate)
>> >>    (-(gross)(* gross tax-rate)))
>> >>
>> >> So I expect the function to calculate as
>> >>
>> >> = (-(240)(* 240 0.15)
>> >> = ( - 240 36)
>> >> = 204
>>
>>
>> Just to be more careful: when you're showing the calculation, make
>> sure to include the use of the function:
>>
>>     (netpay 240 0.15)
>>      = (-(240)(* 240 0.15)
>>     = ( - 240 36)
>>     = 204
>>
>>
>> There's a hitch on the first step in the calculation, and it has to do
>> with the parens.  Unlike its use in traditional math notation, parens
>> are significant in this language: that is, every use of paren has to
>> mean something: it's not superfluous: if you have too many or too few,
>> it changes the meaning of the program.
>>
>>
>> So, within the larger term here:
>>
>>    (- (240) (* 240 0.15))
>>
>> the subterm
>>
>>    (240)
>>
>> means "call the function 240".  That may not be what you intend, but
>> that what it means in this language.
>>
>>
>> You can see this if you go back to what the error message is saying:
>>
>>   function call: expected a defined function name or a primitive
>> operation name after an open parenthesis, but found a function
>> argument name
>>
>> It's basically trying to point out this problem, that the use of
>> "(gross)" within the expression
>>
>>   (-(gross)(* gross tax-rate))
>>
>> is trying to use gross as if it were a function, rather than the
>> numeric argument to netpay.
>>
>
> the subterm
>
>    (240)
>
> means "call the function 240".  That may not be what you intend, but
> that what it means in this language.
>
> Awesome pickup, thank you. Can I ask how I could then specify gross a
> single item in the formula and not as a function?
>

Thank you everyone, I have solved it and thank you for picking me up on the
small intricises.

This is my solution though it works I do feel I "Cheated" somewhat but this
is what I have done and tested working.

(define tax-rate 0.15)
(define pay-rate 12)
(define (gross hours)
  (* hours pay-rate))

(define (netpay gross tax-rate)
    (- gross 0 (* gross tax-rate)))


> (netpay (gross 20)tax-rate)
204
>

Sayth
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20101222/181f7554/attachment.html>

Posted on the users mailing list.