[racket] contracts questions

From: Kevin Forchione (lysseus at gmail.com)
Date: Wed Aug 20 01:44:57 EDT 2014

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



Posted on the users mailing list.