3 ms·
I have some reservations about how this is designed. All we need are columns labeled with conditions. We don't need rows. And the matrix can just have true/fal
by kazinator 9y ago
I have some reservations about how this is designed.
All we need are columns labeled with conditions. We don't need rows. And the matrix can just have true/false/don't-care entries, with code assigned to rows.
Concretely, say we have these conditions:
(> x y) (stringp foo) (oddp n)
Right? Okay, so now we can identify the combinations of these and assign them to code like this:
(> x y) (stringp foo) (oddp n)
#t #t (whatever)
#t #t (other-thing)
#t #f (etc)
There could be a way to mark some of the rows as having "fall through" behavior. If they match, the expression is evaluated (for its side effects, obviously), but then subsequent rows can still match.
This could be worked into a straightforward S-exp syntax without any diagramming shennanigans:
(table-cond
(> x y) (stringp foo) (oddp n)
#t () #t (let [...]
(whatever))
() #t #t (other-thing)
#t #f () (etc))
Here, don't cares are denoted using (). Something else could be chosen.
A #f entry means "must be explicitly false". A blank column entry is a "don't care"; that condition is not taken into account for that row.
- porges 9y agoAt that point aren't you just doing normal Racket pattern matching? (match (list (> x y) (stringp foo) (oddp n)) [(list #t _ #t ) (whatever)] [(list _ #t _ ) (other-thing)] [(list #t #f _ ) (etc)]) ... or is that the joke :)
- kazinator 9y agoThere you go. That just needs a very light syntactic sugar and it's there. The elements of the lists are constants, so you want to use a literal, and the quoting of that can all be hidden. The (list ...) around the incoming expressions can be trivially hidden also. The one thing I suspect match probably doesn't do is feature a fall-through mechanism that I alluded to; say we want (other-thing) for its side-effect, and then still evaluate (etc) if (> x y). (Under no circumstances do we want fall-through to be opt-out, like in the C switch statement with its forgotten break bugs, but opt-in.) Also, this mechanism could be optimized. Since the pattern lists must only contain #t, #f and _, they can be validated to contain nothing else. Accordingly, they can be arithmetically encoded (two bits per symbol), and subject to a numeric dispatch. '(#f _ #t) is a six bit number; '(_ #f #f) is another six-bit number and so on. Arguably, match itself could do that, but it's rather specialized.