[racket] Defining Variables in a function

From: Luke Jordan (luke.jordan at gmail.com)
Date: Sat Jan 1 10:12:23 EST 2011

You're thinking on the right lines.  The book teaches to avoid recalculating
values that are used more than once.  However the method that's used for
this isn't introduced until chapter 18.  Check it out or just work according
to what the book has made available to you at a given point and trust that
in time all will be revealed.

Luke

On Sat, Jan 1, 2011 at 08:03, Sayth Renshaw <flebber.crue at gmail.com> wrote:

> Working through HTDP and I got to this example(below) I created a working
> function but I reused a function, is there a better way to assign a variable
> in another function so, for refernce this was the question.
>
> *Exercise 4.4.2.*   Develop the function tax, which consumes the gross pay
> and produces the amount of tax owed. For a gross pay of $240 or less, the
> tax is 0%; for over $240 and $480 or less, the tax rate is 15%; and for any
> pay over $480, the tax rate is 28%.
>
> My Working solution
>
> ;;; Given a gross wage determine the tax that applies taxed in bands
> ;;; number -> number
> ;;; less than 240, greater than 240 < 480, greater than 480
> (define (tax_pay pay)
>   (cond
>     [(<= pay 240) 0]
>     [(<= pay 480) (* pay 0.15)]
>     [(> pay 480) (* pay 0.28)]))
>
> (define (pay hours)
>   (* 12 hours))
> ;;; Calculate netpay (pay - tax)
> ;;; Pay rate 12 and user input hours
> ;;; (pay (payrate * hours)) - (pay -(tax (pay * tax percent))
> (define (netpay hours)
>   (- (pay hours)(tax_pay (pay hours))))
>
> Proposed solution - assign a variable to the pay calculation and reuse it,
> however it thinks I am redefining the function tax_pay. In embryo this is a
> better way to crack the egg isn't it?
>
> (define (netpay2 hours)
>   (h (pay hours)
>      (- h (tax_pay h))))
>
> _________________________________________________
>  For list-related administrative tasks:
>  http://lists.racket-lang.org/listinfo/users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20110101/3afc463a/attachment.html>

Posted on the users mailing list.