[racket] Pattern matching define macro

From: Neil Van Dyke (neil at neilvandyke.org)
Date: Sat Jul 12 14:38:48 EDT 2014

Complementary to the suggestions that Matthias and Jens Axel made, also 
remember that Racket has a very different evaluation model than Haskell.

Consider reworking the use of the "and" form and everything within it: 
instead, test "r" and "c" for "integer?", and then use "<=" and ">=" to 
determine whether in range.

Otherwise, you're constructing lists of numbers on each call to 
"is-pos".  This might be practically OK in this particular case, since 
the lists will always be small, but it's not something you'd want to do 
if the lists could ever be huge.  And I'd say it's not really idiomatic 
Racket for purposes of your learning exercise.

Remember that Racket is an imperative language; Racket is not Haskell, 
nor is Racket a mathematician. :)

Neil V.

Brian Adkins wrote at 07/12/2014 12:53 PM:
> I'm porting more Haskell code to Racket as a learning exercise. When I got to this line:
>
> isPos (r,c) = elem r [0..4] && elem c [0..r]
>
> I first wrote this:
>
> (define (is-pos r c) (and (member r (lgen 0 4))
>                            (member c (lgen 0 r))))
>
> where lgen is:
>
> (define (lgen m n) (build-list (+ 1 (- n m)) (λ (x) (+ x m))))
[...]


Posted on the users mailing list.