Renato
Renato

Reputation: 13710

How to define a contract that allows either a quoted list or a function?

I have a function whose single argument can be one of:

How to express this as a contract for this argument?

My first guess was:

(or/c expr? list?)

Any better ideas or this is right?

Upvotes: 0

Views: 29

Answers (1)

Atharva Shukla
Atharva Shukla

Reputation: 2137

Since expr? does not exist, you should either use procedure? or something using the arrow constructor (for example (-> number? any/c)) for the function part of the contract.

Moreover, since this is a contract for a function, you should include both domain and range using ->.

Example:

#lang racket

(require racket/contract)
(require rackunit)

(define/contract (f x)
  (-> (or/c (-> number? number?) list?) (or/c number? list?))
  (if (list? x)
      x
      (x 3)))

(check-equal? (f '()) '())
(check-equal? (f add1) 4)

Upvotes: 1

Related Questions