[racket] rudimentary Q. about lambda + contracts

From: Greg Hendershott (greghendershott at gmail.com)
Date: Wed Nov 13 14:36:32 EST 2013

> Having gotten in the habit of writing function contracts, I prefer
> define/contract to (provide (contract-out ...)) because it keeps the
> contract near the function.

I also prefer that very much. Just be aware that `define/contract`
also has the semantic difference of making the contract apply to the
function itself, not just to the function as-provided outside the
module.

Although that may be what you want, it makes the function slower to
call. Obviously from within the module (since a contract now applies).
But even slower from outside the module, compared to using
provide/contract.  I believe it's much less slower in recent Racket
versions, but still at least somewhat slower.

In short, what you may want to do is make your own
`define/contract`-like macro. Such as (typing quickly, not tested):

(define-syntax-rule (define/contract* (id args ...)
                      contract
                      body0
                      body ...)
  (begin
    (provide (contract-out [id contract]))
    (define (id args ...)
      body0
      body ...)))

Use exactly like define/contract.

(define/contract* (x2 x)
  (integer? . -> . integer?)
  (* x 2))

Posted on the users mailing list.