[plt-scheme] A couple of contract questions

From: Matt Jadud (jadudm at gmail.com)
Date: Sat Aug 16 18:58:14 EDT 2008

Hi Gregory,

On Sat, Aug 16, 2008 at 6:30 PM, Woodhouse Gregory
<gregory.woodhouse at gmail.com> wrote:
> How do I write a contract for a function that takes no arguments (i.e., a
> thunk)?

(define/contract foo
  (-> number?)
  (lambda () 42))

> Is it possible to write a contract specifying the return value
> for a function like
> (lambda (x) (lambda (y) (+ x y)))

(define/contract bar
  (-> (-> number? number?))
  (lambda () (lambda (n) 42)))

Or, if you prefer,

(define/contract greg
  (-> number? (-> number? number?))
  (lambda (x) (lambda (y) (+ x y))))

> ((greg 4) 2)
6
> ((greg 4) 'two)
. . 5:4: 5:3 broke the contract
  (-> number? (-> number? number?))
on greg; expected <number?>, given: two

You can be more specific:

(define/contract specific-greg
  (-> (and/c number? even?) (-> (and/c number? odd?) number?))
  (lambda (x) (lambda (y) (+ x y))))

> ((specific-greg 2) 3)
5
> ((specific-greg 3) 2)
. . 5:4: 5:3 broke the contract
  (->
   (and/c number? even?)
   (-> (and/c number? odd?) number?))
on specific-greg; expected <(and/c number? even?)>, given: 3

I think I'm using 402 under OSX.

Cheers,
Matt


Posted on the users mailing list.