[racket] Pattern matching define macro

From: Brian Adkins (racketusers at lojic.com)
Date: Sat Jul 12 16:19:14 EDT 2014

Thanks for the advice Neil. I'm a little confused by your "Racket is an imperative language" statement though - it seems quite "functional" to me, which was one of the attractions.

The Haskell code was written a few years ago, as a relative newbie, as an exercise in emphasizing readability and conciseness, as well as showing how it can be close to mathematical notation, hence:

elem r [0..4] && elem c [0..r]

vs.

r >= 0 && r <= 4 && c >= 0 && c <= r

In other words, I wanted to express that r should be an element of the set [0..4], etc. w/o worrying about performance.

It does seem to be a tendency in Haskell code - for example, the quicksort implementation is beautiful, but doesn't use one of the key ideas of swapping in place. It makes for a great demo, but wouldn't be something you'd want in a performance-sensitive section of code.

I am interested in learning the idioms of the Racket community (especially before contributing code), but for this exercise, I wanted to see how close I could get to the original. A separate exercise would be to solve the problem from scratch in a more idiomatic style. It's a toy program (solves the Cracker Barrel pegboard puzzle):

http://blog.lojic.com/2011/03/10/cracker-barrel-peg-board-puzzle-in-haskell/

I didn't want to spend the time on a fresh look, so a transliteration was what I was shooting for. I'm not going to try and code Haskell in Racket, or convert Racket to Raskell :) But, I do like the fact that Racket seems flexible enough to allow me to steal some good ideas from other languages and incorporate them into my Racket code.

Brian

On Jul 12, 2014, at 2:38 PM, Neil Van Dyke wrote:

> 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.