[plt-scheme] Contract question

From: pedro pinto (pedro.e.pinto at gmail.com)
Date: Tue Jan 24 03:43:34 EST 2006

Sorry I was not clear.  I want to specify a function like map, where the
arity of the mapping function argument is dependent on the rest of the
arguments to map. Your solution does that. But I also want to specify the
return type of the mapping function. I did not see a way to describe the
mapping function return type that does not also restrict the way I can
describe its arguments.

More concretly, consider:

(define (build/join-strings mapping-f separator . lsts)
        (foldl (lambda (st so-far)
                 (format "~a~a~a"
                         so-far
                         (if (equal? so-far "")
                             ""
                             separator)
                         st))
               ""
               (apply map (cons mapping-f lsts)))))

I want to state that mapping-f has the same arity has the rest arguments to
build/join-strings which your solution does, but I also want to say that
mapping-f must return a string.

-pp






On 1/24/06, Robby Findler <robby at cs.uchicago.edu> wrote:
>
> You have to pin down the arity enough that users of your function know
> what the legal argument lengths are. (Is that what you're asking?)
>
> Robby
>
> At Mon, 23 Jan 2006 23:58:08 +0000, pedro pinto wrote:
> >  One more question, how would I go about saying something about the
> return
> > type of proc/func? From my understanding of the docs I need to write a
> > function contract, as opposed to what I think is a flat-contract like
> > arity-matches-lists, but I did not see a way to write one that does not
> also
> > pin down the arity.
> >
> > -pp
> >
> >
> >
> > >  On 1/23/06, Robby Findler <robby at cs.uchicago.edu> wrote:
> > > >
> > > > 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
> > > >
> > >
> > >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20060124/a68ba653/attachment.html>

Posted on the users mailing list.