[racket] Pattern matching define macro

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sat Jul 12 13:17:27 EDT 2014

On Jul 12, 2014, at 12:53 PM, Brian Adkins wrote:

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


Have you considered turning your Haskell code into prefix and leaving it otherwise alone (mostly): 

#lang racket

;; -----------------------------------------------------------------------------
;; prelude 

(module+ test (require rackunit))

(define-syntax-rule
  (= (f x ...) expression)
  (define (f x ...) expression))

(define (.. low high) (range low high))
(define (elem r rng) (cons? (member r rng)))

;; -----------------------------------------------------------------------------
;; Haskell to Racket 
;; isPos (r,c) = elem r [0..4] && elem c [0..r]

((isPos r c) . = . (and (elem r [.. 0 4]) (elem c [.. 0 r])))

;; -----------------------------------------------------------------------------
;; some tests

(module+ test
  (check-true (isPos 1 0))
  (check-true (isPos 2 1))
  (check-false (isPos 1 2))
  (check-false (isPos 5 1)))



Okay, the line is still a bit wider but that's because of extra spaces -- Matthias




Posted on the users mailing list.