[racket] contracts questions

From: Kevin Forchione (lysseus at gmail.com)
Date: Wed Aug 20 15:47:00 EDT 2014

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140820/36ca0c0d/attachment.html>

Posted on the users mailing list.