[racket] contracts questions

From: Roman Klochkov (kalimehtar at mail.ru)
Date: Wed Aug 20 03:02:24 EDT 2014

 > 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)


Tue, 19 Aug 2014 22:44:57 -0700 от Kevin Forchione <lysseus at gmail.com>:
>Hi guys,
>In an attempt to tighten up my code I started using the raise-xxxx-error feature and then began looking at contracts. I like the concept and think it would lead to cleaner looking code But I have two questions:
>
>Why does list? allow a vector in the following contract? Also, how do I indicate an optional (in this case keyword) argument? 
>
>(I’m assuming I’d have to define a contract to handle the valid exact-nonnegative-integer/range for the index, unless there is one already defined. I’ll have a go at that later. )
>
>#lang racket
>
>(provide (contract-out 
>          (insert-at (list? exact-nonnegative-integer? any/c . -> . any))))
>
>(define (insert-at lst index val #:splice (splice #f))
>  #;(unless (list? lst)
>    (raise-argument-error 'insert-at "list?" lst))
>  #;(uqnless (exact-nonnegative-integer? index)
>    (raise-argument-error 'insert-at "exact-nonnegative-integer?" index))
>  #;(unless (<= index (length lst))
>    (raise-range-error 'insert-at "list" "" index lst 0 (length lst)))
>  #;(unless (boolean? splice)
>    (raise-argument-error 'insert-at "boolean?" splice))
>  
>  (define-values (head tail) (split-at lst index))
>  (append head
>          (cond
>            [(and splice (cons? val)) val]
>            [else (list val)])
>          tail))
>
>>(insert-at #(a b) 0 ‘c)
>'(c . #(a b))
>
>Thanks!
>
>-Kevin
>
>
>____________________
>  Racket Users list:
>   http://lists.racket-lang.org/users


-- 
Roman Klochkov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140820/bb080217/attachment.html>

Posted on the users mailing list.