Racket: higher order function contracts

I use the Racket contract system, and I want to export a function with 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 a contract for this kind of function?

+3
source share
1 answer

I suspect it will look something like this:

#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)) - this is a contract that corresponds to functions that return another function, which when calculating returns one integer value.

foo, any any/c, , . :

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

. .

+2

Source: https://habr.com/ru/post/1793526/


All Articles