MathGroup Archive 2002

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

Search the Archive

Programming language difficulties.

  • To: mathgroup at smc.vnet.net
  • Subject: [mg38382] Programming language difficulties.
  • From: Oliver Ruebenkoenig <ruebenko at donne.imtek.uni-freiburg.de>
  • Date: Fri, 13 Dec 2002 04:09:54 -0500 (EST)
  • Organization: Rechenzentrum der Universitaet Freiburg, Germany
  • Sender: owner-wri-mathgroup at wolfram.com

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: PlusMinus Operator Question
  • Next by Date: Re: Plot[x Sin[x],{-100,100}] Bad {-100,99} Good?
  • Previous by thread: RE: looking for a "test[ 1, ALL ]
  • Next by thread: Re: Programming language difficulties.