Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

varaible question

20 views
Skip to first unread message

Roelof Wobben

unread,
Feb 1, 2012, 10:12:50 AM2/1/12
to
Hello,

I have this problem :

Utopia's tax accountants always use programs that compute income taxes
even though the tax rate is a solid, never-changing 15%. Define the
program tax, which determines the tax on the gross pay.

Also define netpay. The program determines the net pay of an employee
from the number of hours worked. Assume an hourly rate of $12.


So in my opion I can solve this at this way :


grosspay = hours_work * 12,5
tax = grosspay * 0,15
netpay = grosspay - tax.

So I did this :

(define (grosspay h)
(* h 12.50))

(define (tax grosspay)
( * grosspay 0.15))

(define (nettoloon grosspay tax)
(- grosspay tax))

But when I do :

(grosspay 1) I get a message that there a 2 variables and one is given.
So the other functions are not executed.

What's the Scheme way to solve this ?

Roelof

Barry Margolin

unread,
Feb 1, 2012, 12:25:17 PM2/1/12
to
In article <5293$4f295674$524b33d0$25...@cache60.multikabel.net>,
It looks to me like that should work, so I think you must have typoed
something. Please post an actual transcript showing exactly what you
did.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

Roelof Wobben

unread,
Feb 1, 2012, 12:45:57 PM2/1/12
to
Hello Barry,

I did typed this.
This is a copy of what I typed in Racket.

Roelof



Op 1-2-2012 18:25, Barry Margolin schreef:

Chris Riesbeck

unread,
Feb 1, 2012, 2:55:52 PM2/1/12
to
I just copy-pasted your code into DrScheme and (grosspay 1) ran and
return 12.5. This is in Beginner Mode. I doubt the Racket update broke
anything this basic. There's something else you're not showing.


Fyndhorn Elder

unread,
Feb 1, 2012, 2:57:16 PM2/1/12
to
Roelof Wobben <rwo...@hotmail.com> writes:

> Hello Barry,
>
> I did typed this.
> This is a copy of what I typed in Racket.
>
(snip)
>>>
>>> grosspay = hours_work * 12,5
>>> tax = grosspay * 0,15
>>> netpay = grosspay - tax.
>>>
>>> So I did this :
>>>
>>> (define (grosspay h)
>>> (* h 12.50))
>>>
>>> (define (tax grosspay)
>>> ( * grosspay 0.15))
>>>
>>> (define (nettoloon grosspay tax)
>>> (- grosspay tax))
>>>
>>> But when I do :
>>>
>>> (grosspay 1) I get a message that there a 2 variables and one is given.
>>> So the other functions are not executed.
>>

There's nothing wrong with this code, trust me.

You could make an AI out of it with e.g. generic methods or
dispatch functions as objects and put it in an banking irc bot :-)

FE

Roelof Wobben

unread,
Feb 1, 2012, 4:15:15 PM2/1/12
to
Oke,

I see it.

I mixed up things I mean (nettoloon 1) is not working.

Roelof


Op 1-2-2012 20:57, Fyndhorn Elder schreef:

Barry Margolin

unread,
Feb 1, 2012, 4:58:56 PM2/1/12
to
In article <d60c7$4f29ab60$524b33d0$11...@cache70.multikabel.net>,
Roelof Wobben <rwo...@hotmail.com> wrote:

> Oke,
>
> I see it.
>
> I mixed up things I mean (nettoloon 1) is not working.
>
> Roelof

nettoloon takes two parameters, grosspay and tax. You've only given it
one.

Your function should take one parameter, hours. It should then call
grosspay to get the gross payment, call tax on that to get the tax, and
then subtract these two amounts to get the net.

Roelof Wobben

unread,
Feb 2, 2012, 1:58:09 AM2/2/12
to
Hello Barry,

That's what I try to do but apperenly not at the right way.

I hope someone can give me a hint how I can take care that the outcome
of the first function can be used at function 2.

Roelof



Op 1-2-2012 22:58, Barry Margolin schreef:

Fyndhorn Elder

unread,
Feb 2, 2012, 2:41:27 AM2/2/12
to
Roelof Wobben <rwo...@hotmail.com> writes:
Hi,
> Oke,
>
> I see it.
>

Right sorry. If you need the grosspay to be called in nettoloon
it gets overrriden by its variable in the define so in the laste define
put another forma parameter name instead of grosspay.

FE

Roelof Wobben

unread,
Feb 2, 2012, 2:51:23 AM2/2/12
to
Op 2-2-2012 8:41, Fyndhorn Elder schreef:
> Roelof Wobben<rwo...@hotmail.com> writes:
> Hi,
>> Oke,
>>
>> I see it.
>>
>
> Right sorry. If you need the grosspay to be called in nettoloon
> it gets overrriden by its variable in the define so in the laste define
> put another forma parameter name instead of grosspay.
>
> FE
>


oke,
but how does Racket then now that the new forma parameter has the same
value as grosspay.

Does anyone have a simple example how I can take care that the value of
function1 get used in function2.

Roelof

Fyndhorn Elder

unread,
Feb 2, 2012, 2:56:15 AM2/2/12
to
Roelof Wobben <rwo...@hotmail.com> writes:

> Hello,
>
> I have this problem :
>
> Utopia's tax accountants always use programs that compute income taxes
> even though the tax rate is a solid, never-changing 15%. Define the
> program tax, which determines the tax on the gross pay.
>
> Also define netpay. The program determines the net pay of an employee
> from the number of hours worked. Assume an hourly rate of $12.
>
>
> So in my opion I can solve this at this way :
>
>
> grosspay = hours_work * 12,5
> tax = grosspay * 0,15
> netpay = grosspay - tax.

Example code for this is :

(define (f hours)
(let* ((gross-pay (* hours 12.5))
(tax (* gross-pay 0.15))
(net-pay (- gross-pay tax)))
(list gross-pay tax net-pay))) ;; this is the return value


FE

Fyndhorn Elder

unread,
Feb 2, 2012, 3:09:39 AM2/2/12
to
Roelof Wobben <rwo...@hotmail.com> writes:

> Op 2-2-2012 8:41, Fyndhorn Elder schreef:
> Right sorry. If you need the grosspay to be called in nettoloon
>> it gets overrriden by its variable in the define so in the laste define
>> put another forma parameter name instead of grosspay.
>>
>> FE
>>
>
>
> oke,
> but how does Racket then now that the new forma parameter has the same
> value as grosspay.
>
> Does anyone have a simple example how I can take care that the value
> of function1 get used in function2.
>

There's lots of ways to do it but here's another :

(define (gross-pay-f h)
(* h 12.50))

(define (tax-f gross-pay-f h)
(* (gross-pay-f h) 0.15))

(define (nettoloon gross-pay-f tax-f h)
(- (gross-pay-f h)(tax-f gross-pay-f h)))

FE

Roelof Wobben

unread,
Feb 2, 2012, 3:21:33 AM2/2/12
to
Thanks ,

But if I put this into Racket I see this message :

<unsaved editor>:5:12: function call: expected a function after the open
parenthesis, but found a variable in: gross-pay-f

Roelof

Jussi Piitulainen

unread,
Feb 2, 2012, 4:36:22 AM2/2/12
to
Roelof Wobben writes:

> Does anyone have a simple example how I can take care that the value
> of function1 get used in function2.

Either you pass the value of function1 to function2 when you call
function2, or you call function1 in the body of the procedure that
implements function2.

The reason to the error message you mentioned is explained by Eli
Barzilay in a recent followup to me: you are using the beginner
setting of Racket, which does not allow procedures as values. You may
not even understand the phrase "procedures as values" yet.

You have seen two different notations for functions in calculus. One
is like y = x^2 + 1, where the parameter x is implicit in y, and the
other is like f(x) = x^2 + 1, where the parameter x is explicit in
f(x). I think you tried to use the former convention and got stuck.
Better use the latter when programming.

Something like this should work:

(define (h x) (- (f x) (g (f x))))
(define (g x) (* x 0.15))
(define (f x) (* x 12))

And when you learn about let and let*, you can improve on it a bit, as
suggested by Fyndhor Elder, or with your current machinery you can use
the fact that x - 0.15 * x = 0.85 * x.

Roelof Wobben

unread,
Feb 2, 2012, 7:39:30 AM2/2/12
to

> Something like this should work:
>
> (define (h x) (- (f x) (g (f x))))
> (define (g x) (* x 0.15))
> (define (f x) (* x 12))
>

Oke,

Thanks for the tip.

Just to be sure.

When function h is executed with argument x.
Then in the calculation area (- f x) (g (f x))
First function f is executed and after that function g with as argument
function f .

Roelof


Jussi Piitulainen

unread,
Feb 2, 2012, 8:05:28 AM2/2/12
to
Let's be pedantic with the parens: (- (f x) (g (f x))).

You got the order of execution right: - is called with two arguments,
the first is the value of (f x), the second the value of (g (f x)). In
the second argument expression, g is called with the value of (f x).

Scheme is said to use call-by-value (eager), as opposed to policies
like call-by-name (old) and call-by-need (lazy) that may not evaluate
argument expressions in this order (or at all).

Catfish

unread,
Feb 2, 2012, 10:15:53 AM2/2/12
to
As this dude says, you're in a REPL, when you put a parens
you call a function, when you have a variable the result gets
printed.

CF

Roelof Wobben

unread,
Feb 2, 2012, 8:20:26 AM2/2/12
to
Everyone thanks for the explanation and patience with me.
I begin to understand how scheme works.

Roelof



Op 2-2-2012 14:05, Jussi Piitulainen schreef:

Catfish

unread,
Feb 2, 2012, 10:34:48 AM2/2/12
to
Roelof Wobben <rwo...@hotmail.com> writes:

> Everyone thanks for the explanation and patience with me.
> I begin to understand how scheme works.
>

Dude, happy hacking!
0 new messages