[racket] Understanding contracts

From: Alexander McLin (alex.mclin at gmail.com)
Date: Wed Oct 1 22:46:16 EDT 2014

Hello,

I've been working on a sample project to better understand how to use the
contract system. I created a simple Bezier curve implementation and am
using (provide (contract-out...) to attach contracts to the provided
bindings.

Basically I have a procedure called cubic-bezier that accepts two control
point structs used to define the curve. It returns another procedure that
actually generates the curve, it accepts an integer parameter that lies on
[0, 1] and an optional keyword #:clamp-range. The generator procedure
returns a number.

The control point structure is a simple posn type that accepts X and Y
fields where X must be between 0 and 1, and Y is allowed to be any number.

Here are the contracts I defined provisionally:

(provide (contract-out
          [struct bezier-control-point ((x (and/c number? (between/c 0 1)))
                                                      (y number?))]
          [cubic-bezier (-> bezier-control-point?
                                     bezier-control-point?
                                    (->* ((and/c number? (between/c 0 1)))
                                           (#:clamp-range boolean?)
                                           number?))]))

For the contract attached to cubic-bezier using ->, my thinking was to use
->* to generate the contract for the procedure returned by cubic-bezier but
it's not working, meaning I'm not getting the any contract violations I'm
expecting when giving bad inputs to cubic-bezier's value.

How can I attach a more complex contract to cubic-bezier's value?

Thank you
Alexander McLin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20141001/4f34db99/attachment.html>

Posted on the users mailing list.