[plt-scheme] Contract question

From: Robby Findler (robby at cs.uchicago.edu)
Date: Mon Jan 23 15:12:39 EST 2006

You have to use the more complex combinators to express that. Here's
how ->r can be used to express map's contract:

(module map-c mzscheme
  (require (lib "contract.ss"))
  
  (define map-c map)
  
  (provide/contract
   [map-c
    (->r ([func 
           (and/c
            procedure?
            (let ([arity-matches-lists
                   (λ (f)
                     (procedure-arity-includes? f (+ 1 (length rest-args))))])
              arity-matches-lists))]
          [lst-one (listof any/c)])
         rest-args
         (and/c (listof (listof any/c))
                (let ([same-length-lists (λ (x) (apply equal? (length lst-one) (map length x)))])
                  same-length-lists))
         (listof any/c))]))

(require map-c)

(define (print-msg exn)
  (display (exn-message exn))
  (newline))

(map-c (lambda (x y) x) (list 1 2 3) (list 4 5 6))
(with-handlers ([values print-msg])
  (map-c (lambda (x) x) (list 1 2 3) (list 4 5 6)))
(with-handlers ([values print-msg])
  (map-c (lambda (x y) x) (list 1 2 3) (list 4 5 6 7)))

At Mon, 23 Jan 2006 19:49:34 +0000, pedro pinto wrote:
> Hi there,
> 
> I just started fumbling around with contracts and I was trying to write a
> contract for a function similar to map:
> 
> map proc clist1 clist2 ... -> list
> 
> Is there a way to say that proc is a procedure whose arity should match the
> number of clist arguments?
> 
> TIA,
> -pp
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme


Posted on the users mailing list.