[racket] Pattern matching define macro

From: Brian Adkins (racketusers at lojic.com)
Date: Sat Jul 12 18:43:33 EDT 2014

On Jul 12, 2014, at 6:19 PM, Daniel Prager wrote:

> Hi Brian
> 
> r >= 0 && r <= 4 && c >= 0 && c <= r
> 
> 
> implies
> 
> 0 <= c <= r <= 4
> 
> 
> Or  using prefix and the variable-arity of <=:
> 
> (define (is-pos r c)
>  (<= 0 c r 4))
> 
> 
> which I think works well for clarity, concision, and efficiency.
> 
> Dan

Very nice observation; I like it. I've always felt the variable-arity of lisp was a cool feature. I switched to a struct for the arg, but the following works:

(defpat (is-pos? (pos r c))
  (<= 0 c r 4))

I probably won't keep my defpat macro, at least not in its present form (for one, it only handles a single arg); there's probably a balance between being concise and being general/flexible.

I finished the program, but it's very raw - I haven't tried to match the Racket style, be idiomatic, etc., just rushed through it to get a rough idea of how it compares:

https://gist.github.com/lojic/14aefacc29ab5a88fa98

I'll come back to it after I get some more experience and improve it. Despite the questionable utility of these exercises, I always run into something that causes me to learn something valuable about Racket.



Posted on the users mailing list.