Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!unmvax!uokmax!d.cs.okstate.edu!norman From: norman@d.cs.okstate.edu (Norman Graham) Newsgroups: comp.lang.functional Subject: Re: Extensional functions in SML? Message-ID: <1990Sep20.074513.19190@d.cs.okstate.edu> Date: 20 Sep 90 07:45:13 GMT References: Organization: Oklahoma State University Lines: 81 From article , by stabl@unipas.fmi.uni-passau.de (Robert Stabl): > In article <26470@boulder.Colorado.EDU>, dennis@boulder.Colorado.EDU (Dennis Heimbigner) writes: >> [Omitted discussion where Dennis wants to create a function dynamically, >> by adding (or removing) input/output information to (or from) the >> function at run time.] Dennis originally asked for something analogous to Prolog's database manipulated with assert and retract. Since this is 'state', and purely functional systems are stateless, you obviously can't have it. Variables do not change their values over time; and since a named function is just a variable bound to a 'function value', that variable must represent the same function for the life of the variable. [This is very basic stuff and is provided only for the benefit of novice functional programmers.] [BTW, the charter of this newsgroup forbids discussion of imperative features found in some 'impure' functional languages. So SML references are out for now.] > [Now, Robert provides a SML example that illustrates what he thinks > Dennis wants. I've omitted some SML responses for brevity.] > > - exception unbound; (* needed for undefined values *) > > - fun update f (x, y) = fn n => if (n = x) then y else f (n); > (* this function updates f with the new pair (x, y) *) > > - fun raise_unbound x = raise unbound; > (* The first update uses this function *) > > - val f = update raise_unbound (1, 2); > (* the first pair *) > > - val f = update f (17, 9); > (* the next one *) > > - val f = update f (23, ~1); > (* and so on *) > > (* now the application of f *) > - f(17); >> val it = 9 : int > > - f(23); >> val it = ~1 : int > > - f(3); >> uncaught exception unbound The choice of names in this example may confuse some novice functional programmers. First, the function 'update' really doesn't update anything: It merely creates a new function value that is a little more defined than the function value passed to it. Second, the three 'val f = ...' expressions are not destructively updating f: Each f is a _different_ name--you might as well have called them f1, f2, and f3. The command interpreter creates a new lexical scope for each definition of f. Think of the above example as let f = update raise_unbound (1, 2) in let f = update f (17, 9) in let f = update f (23, ~1) in [f(17),f(23),f(3)] end end end I hope this clarifies things somewhat. Maybe I just misunderstood the question... Cheers, Norm -- Norman Graham {cbosgd,rutgers}!okstate!norman The opinions expressed herein do not necessarily reflect the views of the state of Oklahoma, Oklahoma State University, OSU's Department of Computer Science, or of the writer himself.