MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Programming language difficulties.

  • To: mathgroup at smc.vnet.net
  • Subject: [mg38441] Re: Programming language difficulties.
  • From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
  • Date: Mon, 16 Dec 2002 02:20:17 -0500 (EST)
  • Organization: Universitaet Leipzig
  • References: <atc9f8$3hs$1@smc.vnet.net>
  • Reply-to: kuska at informatik.uni-leipzig.de
  • Sender: owner-wri-mathgroup at wolfram.com

Hi,

would you be so kind to read the manual *before* you
start to programm ? Fine.

Your first definition 
> withdraw := Evaluate[
>     Module[ { balance = 100 },
>       Function[ amount,
>         If[ balance >= amount,
>           balance -= amount; balance,
>           Print[ "Insufficient Funds" ]
>           ]
>         ]
>       ]
>     ]

return a pure function that does the If[] test when you
call it withdraw[60]. 

Since you force to evaluate the Module[] you create global
variable balance$<a number>  that can't removed when the
Module[] ends. If you would like to use a fixed constant you
should use With[], i.e.

withdraw = 
    With[{balance = 100},
      Function[amount,
        If[balance >= amount,
          balance - amount,
          Print["Insufficient Funds"]]]]

In the next definition

> secondWithdraw[ initBalance_ ] := Evaluate[
>     Module[ { balance = initBalance },
>       Function[ amount,
>         If[ balance >= amount,
>           balance -= amount; balance,
>           Print[ "Insufficient Funds" ]
>           ]
>         ]
>       ]
>     ]

you Evaluate[] the right side before you have a given a value
for initBalance but that can't work, because  the pattern
initBalance_ has absolute nothing to do with the variable
initBalance. You mean

withdraw::insuff = "Insifficuent funds '1'."

withdraw[amount_, balance_:100] :=
  If[balance >= amount,
    balance - amount,
    Message[withdraw::insuff, balance]
   ]

or

withdraw1[amount_,balance_:100]/; balance>amount :=balance-amount
withdraw1[amount_,balance_:100]:=(Message[withdraw::insuff,
balance];balance)

Regards
  Jens


Oliver Ruebenkoenig wrote:
> 
> Hi again Mathgroup ;-)
> 
> This is a programming example from the wizard book chapter 3.
> ( http://mitpress.mit.edu/sicp/full-text/book/book.html )
> 
> Consider the following:
> 
> withdraw := Evaluate[
>     Module[ { balance = 100 },
>       Function[ amount,
>         If[ balance >= amount,
>           balance -= amount; balance,
>           Print[ "Insufficient Funds" ]
>           ]
>         ]
>       ]
>     ]
> 
> In[2]:= withdraw [ 60 ]
> 
> Out[2]= 40
> 
> In[2]:= withdraw[ 60 ]
> Insufficient Funds
> 
> The question now is, can I in Mathematica write a function that takes as
> argument the balance, so that I do not have to use the fixed balance =
> 100. Note that in first example balance is _not_ present in the global
> context.
> 
> My idea was the following:
> 
> secondWithdraw[ initBalance_ ] := Evaluate[
>     Module[ { balance = initBalance },
>       Function[ amount,
>         If[ balance >= amount,
>           balance -= amount; balance,
>           Print[ "Insufficient Funds" ]
>           ]
>         ]
>       ]
>     ]
> 
> In[7]:= W1:=secondWithdraw[ 100 ]
> 
> In[8]:= W1[ 60 ]
> Out[8]= If[initBalance >= 60, balance$2 -= 60; balance$2,
> >    Print[Insufficient Funds]]
> 
> So this however does not work. I _assume_ that Evaluate hits to early.
> The evaluation of balance >= amount to initBalance >= amount is too
> early. Is this the problem? How can I circumvent it?
> 
> I'd be glad for any insights you might have.
> 
> Oliver Ruebenkoenig, <ruebenko at imtek.de>
>    Phone: ++49 +761 203 7293


  • Prev by Date: Re: Numeric Integration of Tabulated Integrand Function: Part I
  • Next by Date: Re: Kernel configuration options for ssh
  • Previous by thread: RE: Programming language difficulties.
  • Next by thread: LabelContourLines bug/feature