[racket] Basic Racket Question
On Dec 21, 2010, at 6:50 AM, Sayth Renshaw wrote:
> I am sure I am trying to over define this but not sure how to use other methods in Racket yet only beginning. So Hi everyone.
>
> Doing the netpay of employee tax = 0.15 and pay = hrs *12.
Follow the recipe.
1) Write a function contract answering the questions
a) what is the name of the function you're defining? Obviously, "netpay".
b) what information does it take in as arguments (i.e. what is different every time?) The tax rate doesn't change, so it's not an argument. The per-hour pay rate doesn't change, so it's not an argument. The number of hours DOES change, so it IS an argument -- of type "number".
c) what information does it return? In this case, obviously a number indicating the amount of money you get in your paycheck after taxes.
So my answer to the "contract" step would be
; netpay : number (hours) -> number (net pay)
2) Write examples of calling the function, with right answers. For almost any problem, you need at least two different examples -- more as the problems get more complicated. I tell my students to start with the easiest examples, choosing the numbers so you can solve them in your head, then move to more difficult ones.
(check-expect (netpay 0) 0) ; if you work no hours, you get no pay and you pay no taxes
(check-expect (netpay 1) 10.20) ; 1 hour -> $12 before taxes, minus $1.80 in taxes
(check-expect (netpay 5) 51) ; 5 hours -> $60 before takes, minus $9 in taxes
3) Write a function skeleton satisfying the syntax rule for defining a function, but with no "body".
(define (netpay hours)
...
)
4) Add an "inventory" listing what you've got available inside the function. For simple functions, this is simply the parameters of the function, with their data types. And if there are particular fixed values that you know you'll need, you can list them here too.
(define (netpay hours)
; hours a number
; 12 a number ($/hour)
; 0.15 a number (tax rate)
...
)
5) Fill in the "body" of the function in place of the "..." above. This will be an expression using things from the inventory -- in your case, you need to use the variable name "hours". I'll leave you to do this one yourself :-)
6) Test the function: does it pass the test cases you wrote back in step 2?
By the way, it is often helpful to put the whole problem on hold and write a "helper function" to do part of the problem. In this case you might want to write a "grosspay" function that takes in a number of hours and computes the before-tax pay. Go through the same series of steps, but the "examples", "inventory", and "body" steps would be simpler than the above. Then return to "netpay", with the additional function "grosspay" available for you to use in the inventory and body.
> (define (hours h)
> (h : number?))
What does ": number?" mean? This might be meaningful in Typed Racket, but we don't usually recommend STARTING in Typed Racket.
More importantly, what is this "hours" function supposed to do? I think you're trying to say "hours is a number", but what you're actually saying is "hours is a function that takes in a number h, and I'm not sure what it returns."
> (define (tax t)
> (= t 0.15))
Again, I think you're trying to say "tax is a number equal to 0.15," but what you're actually saying is "tax is a function that takes in a number t and tells whether or not that number is 0.15."
> (define (payrate p)
> (= p $12.00))
Ditto. BTW, Beginner Racket doesn't recognize "$" in front of a number; if you want the number to represent a number of dollars, you have to adopt that convention outside the language.
> (define (netpay hours tax payrate)
> (* h p)-(* t(* h p)))
What are h, t, and p? You defined things by those names in other functions, but parameters to one function are not visible in another function. The parameters to this function are "hours", "tax", and "payrate", none of which you've actually used.
> Easy Way
>
> (define (netpay hours : number? tax : 0.85)
> (* hours 12)* tax)
This makes intuitive sense: the function takes in a number called "hours", and it uses "tax", which is 0.85, and it multiplies those two things by 12. But it doesn't actually obey Racket syntax:
1) Beginner Racket doesn't take type annotations like ": number?"
2) "tax" doesn't vary, so it shouldn't be a parameter to the function at all.
3) One of the operators in your function body has been written infix rather than prefix.
Good luck!
Stephen Bloch
sbloch at adelphi.edu