[racket] for*/list and Typed Racket

From: Sam Tobin-Hochstadt (samth at ccs.neu.edu)
Date: Mon Jun 25 10:28:41 EDT 2012

On Sun, Jun 24, 2012 at 2:54 PM, Adolfo Pérez Álvarez
<adolfo.pa at gmail.com> wrote:
>
> I've been trying to convert the following code to Typed Racket without success:

Below is a version that typechecks.  There were two problems here.
One, as the error message said, Typed Racket needed more help in the
form of annotations to check your program. Two, you were using
`for*/list` as if it was `for*/fold`.  Unfortunately, the
type-annotated version, `for*/fold:`, doesn't yet support `#:unless`,
so I had to change it to `#:when`.  I'll fix that soon.

#lang typed/racket

(define-type Region Number)
(define-type Color Number)

(define-type Map (Listof (Pair Region (Listof Region))))

(: empty-solution (Listof (Pair Region Color)))
(define empty-solution '())

(: colorize : Map (Listof Color) -> (Listof (Pair Region Color)))
(define (colorize map colors)
 (for/fold ([solutions empty-solution]) ([entry map])
   (match-let ([(cons region neighbours) entry])
     (for*/fold:
       ([s : (Listof (Pair Region Color)) solutions])
       ([c : Color colors]
        #:when (not (findf (λ: ([rc : (Pair Region Color)])
                             (and (memv (car rc) neighbours)
                                  (eqv? (cdr rc) c)))
                           s)))
       (cons (cons region c) s)))))

-- 
sam th
samth at ccs.neu.edu


Posted on the users mailing list.