Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sample.eng.ohio-state.edu!purdue!mentor.cc.purdue.edu!noose.ecn.purdue.edu!samsung!usc!snorkelwacker.mit.edu!ai-lab!opal!sphinx!mfx From: mfx@sphinx.opal (Markus Freericks) Newsgroups: comp.lang.functional Subject: Re: Help needed with behaviour of SML Message-ID: Date: 27 Apr 91 11:00:22 GMT References: <9104262016.AA28063@enuxha.eas.asu.edu> Sender: mfx@opal.cs.tu-berlin.de Organization: /home/pdv/mfx/.organization Lines: 35 In article <9104262016.AA28063@enuxha.eas.asu.edu> surendar@ENUXHA.EAS.ASU.EDU (Surendar Chandra) writes: > > val a = 1; > > a ; > | 1 : int ; > > fun b c = c * a ; > > b 2 ; > | 2 : int ; > > val a = 3; > > b 2 ; > | 2 : int ; > ^^^^^^^^ Why does it do it like this.. Why doesn't it look up at the current > value of the variable 'a'. Why does it store the current value of 'a' in the > function during compilation itself? WHat is the logic behind this? I could > not lay my hands on a good reference for SML and most of the books don't seem > to talk about this at all. ML is a 'real', i.e. 'single-assignment' language. Once a variable is defined, it cannot change its value. So, when you define a=1, then a=3, the second time you create a new variable hiding the old one. So, ML not being dynamically scoped, the function b has as its closure the environment at the time of its creation, i.e. seeing only the 'old' a. You need not think that that value of a is compiled into the function b (although this could be done); its simply a consequence of not being able to alter something once its created. Hope that helps, Markus -- Markus Freericks phone: +49-30-4034110 Oranienburger Str. 142 email: mfx@opal.cs.tu-berlin.de 1000 Berlin 26 FRG or mfx%tubopal@DB0TUI11.BITNET