💾 Archived View for idiomdrottning.org › fanciest-define-gets-even-more-awesome captured on 2023-06-16 at 16:28:51. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-03-20)

-=-=-=-=-=-=-

The fanciest define of all time gets even more awesome

This is a new version of the fanciest define of all time a.k.a. match-generics.

match-generics

Destructuring in the car position

The 1.X releases could not destructure in car position.

So stuff like:

(define ((foo bar) (? string? baz)) ...)

did work, but that (foo bar) had to be the same for every definition, and you could not use matchable’s various features in the car position either. You could have:

(define ((foo bar) (? string? baz)) ...)
(define ((foo bar) a b c)) ...)

But you could not then also have:

(define ((foo bar baz) a b c)) ...)

Match-generics 2.1 removes this limitation.

Now this stuff works:

(define ((apple _) x) x)
(define ((apple 'twisted) x) ((as-list reverse) x))

Faster

2.1 does a better job at clearing out unused branches but also swaps in case-lambda when it doesn’t need the full power of matchable. (And, just like the 1.X series, it swaps in vanilla lambda when it doesn’t need any fanciness at all.)

Backtracking and logic programming

Dispatching isn’t just on the args anymore.

The new version can also backtrack from specializations into generic fallbacks with the new require function.

(define (mysterious a b) 'vanilla)

(define (mysterious a b)
  (require even? (* a b))
  (require even? (+ a b))
  (require (< a b) 'neapolitan))

(map mysterious
     '(1 2 2)
     '(2 3 4))

⇒ (vanilla vanilla neapolitan)