On Thu, Dec 1, 2016 at 7:29 PM, David Storrs <david.sto...@gmail.com> wrote:

> I'm having trouble understanding the docs on hash contracts (
> https://docs.racket-lang.org/reference/data-structure-
> contracts.html#%28def._%28%28lib._racket%2Fcontract%
> 2Fprivate%2Fhash..rkt%29._hash%2Fc%29%29)
>
> What I'm trying to express is:
>
> - This function returns #t because it is a simple test function intended
> to get the hang of hash contracts...
> - This function takes one argument...
> - Which is a hash...
> - Which has keys 'success, 'file-id, 'scratchdir-path, and 'chunk-hash...
> - All of which are symbols...
> - The values will be, respectively:
> success boolean?
>

1. Like Alexis says, `hash/dc` lets you specify a relationship between keys
and values **but** the same relationship needs to hold for every key/value
pair. Here's an example:

#lang racket
(require rackunit)

(define my-hash/c
  (hash/dc [k char?] [v (k) (=/c (char->integer k))]))

(contract my-hash/c (make-hash '((#\A . 65) (#\B . 66))) '+ '-)
;; good hash

(contract my-hash/c (make-hash '((#\A . 66))) 'pos 'neg)
;; bad hash


2. And here's a funny way to check the property you care about

(define special-list/c
  (list/c (cons/c 'chunk-hash string?)
          (cons/c 'file-id (or/c #f exact-positive-integer?))
          (cons/c 'scratchdir-path path-string?)
          (cons/c 'success boolean?)))

(define (special-hash/c h)
  (special-list/c (sort (hash->list h) symbol<? #:key car)))

;; example

(contract special-hash/c
  (make-hash (list (cons 'file-id #f)
                   (cons 'success #t)
                   (cons 'chunk-hash "")
                   (cons 'scratchdir-path (find-system-path 'temp-dir))))
  '+ '-)

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to