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.<div>
<br></div><div>Luke<br><br><div class="gmail_quote">On Sat, Jan 1, 2011 at 08:03, Sayth Renshaw <span dir="ltr"><<a href="mailto:flebber.crue@gmail.com">flebber.crue@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
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.<br><br><b>Exercise 4.4.2.</b>
Develop the function <code><span>tax</span></code>, 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%.<br><br>My Working solution<br><br>;;; Given a gross wage determine the tax that applies taxed in bands<br>;;; number -> number<br>;;; less than 240, greater than 240 < 480, greater than 480<br>
(define (tax_pay pay)<br> (cond<br> [(<= pay 240) 0]<br> [(<= pay 480) (* pay 0.15)]<br> [(> pay 480) (* pay 0.28)]))<br><br>(define (pay hours)<br> (* 12 hours))<br>;;; Calculate netpay (pay - tax)<br>
;;; Pay rate 12 and user input hours<br>;;; (pay (payrate * hours)) - (pay -(tax (pay * tax percent)) <br>(define (netpay hours)<br> (- (pay hours)(tax_pay (pay hours))))<br><br>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?<br>
<br>(define (netpay2 hours)<br> (h (pay hours)<br> (- h (tax_pay h))))<br>
<br>_________________________________________________<br>
For list-related administrative tasks:<br>
<a href="http://lists.racket-lang.org/listinfo/users" target="_blank">http://lists.racket-lang.org/listinfo/users</a><br></blockquote></div><br></div>