[plt-scheme] Re: MrEd - Gui

From: Matt Jadud (jadudm at gmail.com)
Date: Sun Sep 30 16:36:08 EDT 2007

Hi Laurent,

On 9/30/07, Laurent <Ouaibou at gmail.com> wrote:
> I understood your last post, but not how to write a contract for a
> function of zero argument.
>
> What's a contract ?

Good question. For further reading, see:

http://www.htdp.org/

However, the short version: it tells us what types of information are
going into your function, and what types of information are going out
of your function. In languages like Java, the contract of a function
is fairly explicit:

void some_fun (int x) { ... }

The language tells us that the input to the function is a Java int,
and the return value is void.

Some people, when working in languages like Scheme, tend to write a
type statement in a comment above the function definition that says
what types of information should go in and come out. In PLT Scheme,
there is even a mechanism for making these part of the execution of
your program (see "contracts" in the Help Desk). However, as a step,
you might say a contract on a Scheme function might look like:

;; CONTRACT
;; some-fun : integer -> void
(define (some-fun ...) ...)

This tells me that the function should only take in integers, and the
return value is void. Now, when I have a problem like you had, I might
have a starting point for figuring out where things went wrong...
since you might argue that your error is a contract violation. That
is, you were trying to invoke the result of calling a function, but
the contract did not say that a function was returned by it.

Then again, you might say otherwise. You might just say you made a
syntax error. Either way, your style is your own, but this is a useful
tool, I think, for keeping your code clear.

Cheers,
Matt


Posted on the users mailing list.