Article View: comp.lang.functional
Article #462Re: Extensional functions in SML?
From: Robert Stabl
Date: Thu, 20 Sep 1990 03:49
Date: Thu, 20 Sep 1990 03:49
62 lines
2949 bytes
2949 bytes
>>>>> In article <26470@boulder.Colorado.EDU>, dennis@boulder.Colorado.EDU (Dennis Heimbigner) writes:
Sorry for the long citation but it is needed for better understanding:
Dennis> I have only recently begun to look at SML and I am curious
Dennis> as to the best method for representing functions that are
Dennis> defined by specifying a set of pairs of values, where the set contents
Dennis> varies over time. This is more-or-less analogous to prolog assert/retract
Dennis> or to functional data base model base functions.
Dennis> For example, I want to associate the set of pairs:
Dennis> (1,2)
Dennis> (17,9)
Dennis> (23,-1)
Dennis> with the function f, such that e.g, f(17) = 9, and so on.
Dennis> Then I want to be able to change this set by replacing some
Dennis> pairs and have the function f reflect these changes.
Dennis> The only way I can see to do this in SML is to
Dennis> associate a variable, f_var, with f such that f_var
Dennis> contains the set of pairs. Modifying the set then
Dennis> consists of reconstructing the set and re-storing it into
Dennis> the variable.
This could be *one* way. But, since SML is a *functional* language, try
the following (the input follows the '-', SML output the '>'), the following
functions were written using "Standard ML of New Jersey, Version 0.56":
- exception unbound; (* needed for undefined values *)
> exception unbound
- fun update f (x, y) = fn n => if (n = x) then y else f (n);
> val update = fn : (''a -> 'b) -> ''a * 'b -> ''a -> 'b
(* this function updates f with the new pair (x, y) *)
- fun raise_unbound x = raise unbound;
> val raise_unbound = fn : 'a -> 'b
(* The first update uses this function *)
- val f = update raise_unbound (1, 2);
> val f = fn : int -> int
(* the first pair *)
- val f = update f (17, 9);
> val f = fn : int -> int
(* the next one *)
- val f = update f (23, ~1);
> val f = fn : int -> int
(* 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
Hope this helps.
--
>>>>>>>>>> Robert Stabl <<<>>> stabl@unipas.fmi.uni-passau.de <<<<<<<<<<
Technical Details
Message-ID:
<STABL.90Sep19204925@trillian-gw.fmi.uni-passau.de>Path:
arc1-ca.pugleaf.net!utzoo!attcan!uunet!cs.utexas.edu!sun-barr!apple!snorkelwacker!ira.uka.de!fauern!forwiss.uni-passau.de!unipas.fmi.uni-passau.de!stablReferences:
<26470@boulder.Colorado.EDU>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!cs.utexas.edu!sun-barr!apple!snorkelwacker!ira.uka.de!fauern!forwiss.uni-passau.de!unipas.fmi.uni-passau.de!stabl From: stabl@unipas.fmi.uni-passau.de (Robert Stabl) Newsgroups: comp.lang.functional Subject: Re: Extensional functions in SML? Message-ID: <STABL.90Sep19204925@trillian-gw.fmi.uni-passau.de> Date: 20 Sep 90 03:49:25 GMT References: <26470@boulder.Colorado.EDU> Sender: usenet@forwiss.uni-passau.de (USENET News System) Organization: Dept. of Comp. Sci., University of Passau, (West-)Germany Lines: 62 In-Reply-To: dennis@boulder.Colorado.EDU's message of 18 Sep 90 18:04:13 GMT