Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!iuvax!purdue!ames!apple!agate!ucbvax!hplabs!otter!ijd From: ijd@otter.hpl.hp.com (Ian Dickinson) Newsgroups: comp.lang.misc Subject: Re: multi-dimensional switch statements Message-ID: <2400025@otter.hpl.hp.com> Date: 25 Jul 89 09:27:50 GMT References: <26584@agate.BERKELEY.EDU> Organization: Hewlett-Packard Laboratories, Bristol, UK. Lines: 57 > /otter:comp.lang.misc / mwm@mica.berkeley.edu (Mike Meyer) / > The basic idea is to be able to code a state table as is, so that the > table is obvious. A two-dimensional switch statement is the analogy > that comes to mind. A typical example might look like: > > > switch (row:bumpx, col: bumpy) { > > case 0: case 3: case 5: case 7: > > case 2: z = x++ + y; z = 2 * x + y; z = 3 * x++ + y; z = x; > case 4: z = x + y; z = x + y++; z = 3 * x + y++; z = y; > case 7: z = x++ + y++; z = x + 2 * y; z = 8 * x++ + y++; z = x; > > } From your example, it looks as though you don't need anything more than pattern matching on the left-hand side of an equation: Eg, Prolog state_machine( 0, 2, Z ) :- Z is state_machine( 0, 4, Z ) :- Z is state_machine( 0, 7, Z ) :- Z is state_machine( 3, 2, Z ) :- Z is ... etc or, SML fun stateMachine 0 2 = | stateMachine 0 4 = | stateMachine 0 7 = | stateMachine 3 2 = ... etc Compiler technology for both languages is maturing, so they could each do a pretty good job of optimising the tests. I have left out the details of the since good programming practice in each language avoids scungy imperative operators like ++. Adding the extra parameters to do it properly would obscure the point. Once upon a different life, I would have preferred to use Prolog. However, having been writing SML code for some time now, I like the mental hygiene of keeping the expressions type-correct, and having the compiler enforce that for me. In the above example, you wouldn't have to keep to integers for the case labels, but each parameter (effectively, every column in the above) would have to be the same type. In return, the compiler will warn when you have incomplete coverage of that type. If I missed the point of the question, please explain further. Cheers, Ian. | Ian Dickinson, HP Labs, Information Systems Centre, Bristol, England | | net: ijd%otter@hplabs.hp.com | | | or: ijd@hplb.uucp | ?- mind(X), body(X), spirit(X). | | These opinions are all my own work. | |