[racket] contracts questions

From: Robby Findler (robby at eecs.northwestern.edu)
Date: Wed Aug 20 22:31:58 EDT 2014

Kevin: one thing to keep in mind when working with contracts is that
they come with a notion of a boundary -- this is critical for proper
blame assignment; without being able to divide up your program into
different pieces, blame assignment cannot point to a specific piece.

When you use define/contract, the boundary is drawn around that single
definition, excluding the rest of the file. When you use
'contract-out' in a provide, the boundary is drawn around the entire
module. Usually, but not always, the module is a more natural
division, especially if you keep your modules relatively small.

This is why you didn't see any contract checking at the REPL with the
code from your earlier message; the REPL counts as "inside" the
module.

hth,
Robby

On Wed, Aug 20, 2014 at 2:47 PM, Kevin Forchione <lysseus at gmail.com> wrote:
>
> On Aug 20, 2014, at 12:02 AM, Roman Klochkov <kalimehtar at mail.ru> wrote:
>
>> Why does list? allow a vector in the following contract?
>
> Contract in `provide' is used only when you `require' your module from
> other. If you want always test it, use define/contract
>
>> Also, how do I indicate an optional (in this case keyword) argument?
>
> http://docs.racket-lang.org/reference/function-contracts.html#%28form._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._-~3e%2A%29%29
>
> (->* (list? exact-nonnegative-integer?) (#:splice boolean?) any)
>
>
> Thanks! That got me started. This looks much better than what the
> raise-xxxx-error approach:
>
> ;; subseq: list number number -> list
> ;; returns the list of length len starting from pos elements of lst.
> ;;
> ;; Examples:
> ;;
> ;;    > (subseq '(a b c d e f g) 3 2)
> ;;    '(d e)
> (define/contract (subseq lst pos (len (- (length lst) pos)))
>   ; Contract (man ...) (opt ...) (result)
>   (->i ([lst list?] [pos (lst) (integer-in 0 (length lst))])
>        ([len (lst pos) (integer-in 0 (- (length lst) pos))])
>        (result list?))
>   (take (drop lst pos) len))
>
> -Kevin
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>

Posted on the users mailing list.