| > I would be interested in seeing anything that was shorter One way to do that would be to pose that as a problem on the Code Golf Stackexchange. My rewrite-case solution condenses (by removal of all non-essential spaces) to 69 bytes if the symbols are in a hash table K, and the input list is in a variable y, rather than a big literal: (rewrite-case x y((@[K @sym]* . @rest)^(,sym _ . ,rest))(@else else))
A one-letter name could be chosen for the macro. Furthermore, one-letter names could be chosen for sym, rest and else: 20> (r x y((@[K @s] * . @r)^(,s _ . ,r))(@e e))
(foo bar * select _ fox where _ bravo)
Still working! Now down to 43 bytes.(It's not because I cannot that I do not do this with all my code.) Doh, why use . ,r in the backquote if we are golfing? That should be ,*r: using the splice operator, like Common Lisp's or Scheme's ,@, getting us to 42 bytes: 20> (r x y((@[K @s] * . @r)^(,s _ ,*r))(@e e))
(foo bar * select _ fox where _ bravo)
I suspect Code Golf Stackechange regulars could get it down to way in one of the dedicated golfing languages like Retina or what have you.*What else can we do? The @[K @s] predicate syntax could be replaced by a custom operator defined by defmatch, looking like @(K s). 21> (defmatch k (sym) ^@[K (sys:var ,sym)])
k
22> (r x y((@(k s) * . @r)^(,s _ ,*r))(@e e)) ;; 41
(foo bar * select _ fox where _ bravo)
Just noticed the ) * is not fully golfed: 23> (r x y((@(k s)* . @r)^(,s _ ,*r))(@e e)) ;; 40
(foo bar * select _ fox where _ bravo)
The k pattern operator macro could be non-hygienic: it could implicitly bind a variable called s: 24> (defmatch k () ^@[K @s])
k
25> (r x y((@(k)* . @r)^(,s _ ,*r))(@e e)) ;; 38
(foo bar * select _ fox where _ bravo)
These last few feel like cheating because they are too special purpose. Defining anything you can only possibly use just once isn't making the overall program smaller. |