[racket] Basic Racket Question
On Dec 21, 2010, at 6:50 AM, Sayth Renshaw wrote:
> Doing the netpay of employee tax = 0.15 and pay = hrs *12. From the beginner tutorial. I see two ways a simple and a hard way but neither work.
>
> Hard way
>
> (define (hours h)
> (h : number?))
> (define (tax t)
> (= t 0.15))
> (define (payrate p)
> (= p $12.00))
> (define (netpay hours tax payrate)
> (* h p)-(* t(* h p)))
Something else I forgot to mention in my previous message: you're quite right to want to give names to the tax rate and the hourly pay. The usual way to do this in Beginner Racket would be
(define tax-rate 0.15)
(define pay-rate 12)
Note that these definitions are OUTSIDE the definition of "netpay".
You can then use these in functions you define. For example, the "inventory" step of this function definition would become
(define (netpay hours)
; hours a number
; pay-rate a number ($/hour)
; tax-rate a number
...
)
and then the body could use all three of these variable names.
Later in the book, you'll learn another way to do it, using "local variables" so the names "tax-rate" and "pay-rate" are visible only INSIDE "netpay":
(define (netpay hours)
(local [(define tax-rate 0.15)
(define pay-rate 12)]
; hours a number
; tax-rate a number
; pay-rate a number ($/hour)
...
))
This version doesn't work in Beginning Student Language, though; you need Intermediate Student Language.
Stephen Bloch
sbloch at adelphi.edu