我正在使用 Racket 契约(Contract)系统,我想导出一个没有参数的函数,它返回一个没有参数的 lambda 表达式,e。例如:
#lang racket
(define (foo)
(do-somthing)
(lambda ()
(do-other things)))
有谁知道这种功能的合约怎么写?
最佳答案
我怀疑它看起来类似于:
#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))
是一个合约,它匹配返回另一个函数的函数,该函数在求值时返回单个整数值。
但是如果你想放宽 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
)))
参见 Contracts on Higher-order Functions在 Racket 文档中。
关于functional-programming - Racket:关于高阶函数的合约,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5063047/