[plt-scheme] A couple of contract questions
Let me also reccomend the chapter on contracts in the Guide.
Robby
On 8/16/08, Matt Jadud <jadudm at gmail.com> wrote:
> 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
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
>