Racket: contracts on higher-order functions

1k views Asked by At

I am using Racket contract system, and I want to export a function of no arguments, which returns a lambda expression with no arguments, e. g.:

#lang racket
(define (foo)
  (do-somthing)
  (lambda ()
    (do-other things)))

Does anyone know how to write contract for this kind of function?

1

There are 1 answers

4
Yasir Arsanukayev On BEST ANSWER

I suspect it would look something along the lines of:

#lang racket/load

(module m racket
  (provide/contract [foo (-> (-> any/c))])
  (define (foo)
    (+ 10 3) ; do something
    (lambda ()
      (+ 40 2) ; do other things
      )))

(module n racket
  (require 'm)
  ((foo)))

(require 'n)

(-> (-> any/c)) is a contract that matches functions that returns another function, which, when evaluated, returns a single integer value.

But if you'd like to relax return values of foo, you'd use just any instead of any/c, which allows any number of return values, not just a single value. Consider:

(module m racket
  (provide/contract [foo (-> (-> any))])
  (define (foo)
    (+ 10 3) ; do something
    (lambda ()
      (values (+ 40 2) 666); do other things
      )))

See Contracts on Higher-order Functions in Racket documentation.