From lliao@phobos.cis.udel.edu Mon Oct 7 14:17:58 2002 Date: Mon, 7 Oct 2002 14:17:03 -0400 (EDT) From: Li Liao To: Damian Dechev Subject: Re: HW2 question > Dear Dr. Liao, > > thank you very much for your answer, it helped me a lot. > Now, I am struggling with loop/exit, I've tried many ideas but one of > the most exotic and interesting was to define loop and exit as: > | exec Loop (v::vs,ds) = ( (execute (getProc(v)) (vs,ds)) > handle Exit (vs,ds) => (vs,ds) ) > | exec Exit (v::vs, ds) = raise Exit (vs,ds) => (vs,ds) > > this code is incorrect, the way it is, but do you think this kind of > approach might be useful. If yes, what am I missing .... The loop/exit is probably the hardest part in this assignment; it touches upon an important issue: with no global variables in ML to serve as flags, how is it possible for two functions (or subroutines) to communicate between each other. The idea above is not just "exotic" but marvellous also. While the code is not syntactically correct yet, it does present a "hacking" way to the problem. Remember I mentioned in the class that ML exceptions play a role similar to that of global variables. Here is a good example how that property is used in practice! First you need to declare an exception at the top level, say, exception ExitSignal of State; Then you just raise such an exception when you "exec" Exit, and catch and handle an ExitSignal exception during the executation of a Loop. Remember that exception constructors can take arguments, as in this case. As you can see now, your code is quite close already. Li