如何在Racket/Plait中给一个特定类型的函数作为参数?

How to give a specific type of function as a parameter in Racket/Plait?

我正在编写一个函数,它接受一个函数和一个列表作为参数。参数函数和列表必须具有相同类型的值。我如何确保?

我试过:

(define ( (func -> 'a) [lst : (Typeof 'a)])
     ....)

但是,我无法让它工作。我也浏览了辫子教程,但没有找到任何相关内容。

是否有可能接受特定 return 类型的函数?

这是您要找的吗?

(define f : (('a -> 'a) (listof 'a) -> string)
  (lambda (func lst) "hello"))

然后:

(f (lambda ([x : number]) x) (list 1))

类型检查,但是:

(f (lambda ([x : number]) x) (list "foo"))

不做类型检查,因为'a统一为string(来自"foo"),但也统一为number(来自x),所以出现类型不匹配.

请注意

(define f : (('a -> 'a) (listof 'a) -> string)
  (lambda (func lst) "hello"))

(define (f [func : ('a -> 'a)] [lst : (listof 'a)]) : string
  "hello")

不同。在前者中,'a 指的是跨参数的同一类型变量。在后者中,func'alst'a是不同的。因此,在后者中,以下表达式类型检查:

(f (lambda ([x : number]) x) (list "foo"))