For something like that, I think you don't need to mess with any of
those fancier constructs. You can just use a predicate function
directly as a contract. Here's an example:

#lang racket
(provide
 (contract-out
  [f (-> starts-with-slash? char?)]))

(define (starts-with-slash? s)
  (and (string? s)
       (not (equal? s ""))
       (equal? (string-ref s 0) #\])))

(define (f x)
  (string-ref x 0))

You can also use regular expressions as contracts. They recognize more
than just strings, so to express that same contract you would write
something like this:

(provide
 (contract-out
  [f (-> (and/c string? #rx"^/") char?)]))

As for generators, those are used to be able to randomly generate
examples, which can be useful for testing functions.

If you want to see an example of how that's used, here's one from the
documentation:

  
http://docs.racket-lang.org/reference/Random_generation.html#%28def._%28%28lib._racket%2Fcontract..rkt%29._contract-exercise%29%29

It shows how you can use the random generation to get a function to
break it's own contract. It is useful for testing.

And for a more careful introduction to the contract system overall,
you want to start with these docs, not the ones in the reference.

  http://docs.racket-lang.org/guide/contracts.html

hth,
Robby


On Sat, Jun 4, 2016 at 11:51 PM, Chris Bui <christopher.d....@gmail.com> wrote:
> I'm trying to create a simple flat contract of my own and a generator for it 
> and I really don't know where to start. The docs here: 
> https://docs.racket-lang.org/reference/Random_generation.html aren't very 
> useful for somebody that doesn't know at a high level what to do.
>
> Also, on this page: 
> https://docs.racket-lang.org/reference/data-structure-contracts.html, it says
>
>> The generator argument adds a generator for the flat-named-contract. See 
>> contract-generate for more information.
>
> I can't find contract-generate anywhere in the docs or via Google.
>
> Here's what I'm trying to do:
>
> I have a REST API at work written in another language and I'd like to be able 
> to hit it with requests with lots of different combinations of parameters. My 
> second goal is to be able to verify that the data coming back in json 
> conforms to some schema. Essentially, if the value at a certain key is of a 
> certain type. I did some research and thought that contracts were perfect for 
> me. Am I correct about this?
>
> I started by trying to create a flat-named-contract that checks if the first 
> char of a string is a '/', like for a relative url or file path. I can define 
> the contract, but the generation aspect has been a wall for me.
>
> Could somebody give me an example?
>
> Thanks!
>
> --
> 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.

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