🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.misc
Article #335

Re: multi-dimensional switch statements

#335
From: Mike (I'll think of something ye...
Date: Wed, 26 Jul 1989 04:59
53 lines
3054 bytes
In article <2400025@otter.hpl.hp.com> ijd@otter.hpl.hp.com (Ian Dickinson) writes:
<> /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
<
<From your example, it looks as though you don't need anything more than
<pattern matching on the left-hand side of an equation:
<
<If I missed the point of the question, please explain further.

I would like to thank those who answered, either here or by e-mail.

The original problem was poorly stated - mostly because I wasn't clear,
myself, on what the problem _was_. I now have a solution. I expect
theres a better one, but this one works.

Problem: Given two variables a, b that each have some finite set of A
& B, to be able to specify code to execute for each element of the AxB
set of possible values the two variables could have.

Goals: The user need not mention the possible values A and B more than
once, and that the must not be evaluated more than once.

The problem is, at heart, a notational problem: is there a notation
that fits the current 1-d array of characters that programming
languages are being forced into that would allow the obvious
"tableness" to show through, and still meet the goals above.

A variety of people pointed out that vectors (in various flavors) can
be used to solve this problem. These suffer from the user having to
specify translations from A & B to the indices of the vectors.  Others
suggested variations on associative arrays, which avoids that problem.
However, these require the mention or evaluation of the elements of A
& B multiple times.

The solution appears to be to provide a template for the code
fragments in the switch. Staying with C, this would work in a manner
similar to array initialization. The first "dimension" of the switch
has unknown length, and labels specified by case's as they occur. The
other dimensions have labes/lengths specefied by a template at the
start of the switch, and each case statement marks the beginning of an
N-1d slice through the Nd table.

It's still not pretty, but it hands the whole mess to the compiler to
choose a good implementation for that example.

	Thanx,
	<mike
--
Il brilgue: les toves lubricilleux			Mike Meyer
Se gyrent en vrillant dans le guave,			mwm@berkeley.edu
Enmimes sont les gougebosqueux,				ucbvax!mwm
Et le momerade horsgrave.				mwm@ucbjade.BITNET
Technical Details
Message-ID: <26654@agate.BERKELEY.EDU>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!apple!agate!eris.berkeley.edu!mwm
References: <26584@agate.BERKELEY.EDU> <2400025@otter.hpl.hp.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!apple!agate!eris.berkeley.edu!mwm
From: mwm@eris.berkeley.edu (Mike (I'll think of something yet) Meyer)
Newsgroups: comp.lang.misc
Subject: Re: multi-dimensional switch statements
Message-ID: <26654@agate.BERKELEY.EDU>
Date: 26 Jul 89 04:59:32 GMT
References: <26584@agate.BERKELEY.EDU> <2400025@otter.hpl.hp.com>
Sender: usenet@agate.BERKELEY.EDU
Reply-To: mwm@eris.berkeley.edu (Mike (I'll think of something yet) Meyer)
Organization: Missionaria Phonibalonica
Lines: 53