Hi, Racket Discourse.
I was browsing the examples of the proposed conditional syntax in this paper, The Ultimate Conditional Syntax.
I have not read the formal claims and proofs they make, but the shape of things is intriguing, and it got me wondering about what I would like from the "ultimate conditional".
Arguably, match
and friends, not to forget the behemoth, cond
, are already more than any programmer might ask for; it is, however, fun to speculate.
So, stealing some ideas from the paper, and without stating that this is necessarily better, I would propose something like this:
; cond-case, which is like `cond`
(choose
[is (blue? x) and is (red? y)
then (orange)]
[is (red? x) and is (blue? y)
then (purple)]
[else (yellow)])
; match-case, which is like `match`
(choose
[(choose
[is (odd? a) then (list 1 2)]
[else (list 2 1)]
) is (list x y)
and
b is (list u v)
then (list x u)]
[(choose
[is (even? a) then (cons 1 2)]
[else (cons 2 1)]
) is (cons x y)
and
b is (cons u v)
then (list y v)]
[else (list a b)])
The basic form of choose
is:
(choose <branch> ...+)
where <branch>
is of the form [<case> then <result>]
, or possibly in the final branch, of the form [else <result>]
. Finally, each <case>
is of the form <clause> {and <clause>} ...
. The <result>
would be the same as with cond
and so on.
Each <clause>
can be one of two flavors:
is x
(wherex
is either true-thy or false)x is y
(wherex
is some value andy
is amatch
pattern)
What I like about this, is that it reads quite easily (in these simple cases) and it combines cond
and match
quite elegantly, but it seems like it could become a soup of words quite quickly.
Edit: what about plurals, eh?
(choose
[are (even? x y z) then 'all-even])
; is synonym to something like
(choose
[is (even? x) and is (even? y) and is (even? z) then 'all-even])
(choose
[(x y z) are ((? integer?) (? symbol?) (? string?))
then 'integer-symbol-string])
; is synonym to
(choose
[x is (? integer?) and y is (? symbol?) and z is (? string?)
then 'integer-symbol-string])
Edit: and then you could specialize on the verb! Latin, anyone?
(choose
[is-list (even? xs) then 'all-even])
; is synonym to
(choose
[xs is (list (? even?) ...) then 'all-even])
Edit: instead
(choose
[is list of (blue? '(blue blue blue))
then 'blues]
[else
'sunshine])
;=> 'blues
which expands into a for/and
in terms of the <type>
to range over, being list
in this case, thus:
(for/and ([tmp (in-list vals)]) (pred? tmp))
What do you think about this, and more interestingly, what would your fantasy conditional look like, if any?
Edit: chef's kiss