我正在编写一个函数,它接受一个函数和一个列表作为参数。参数函数和列表必须具有相同类型的值。我如何确保这一点?
我努力了:
(define ( (func -> 'a) [lst : (Typeof 'a)])
....)
但是,我无法让它工作。我也浏览了辫子教程,但没有发现任何相关内容。
是否有可能拥有一个接受特定返回类型的函数的函数?
这是你想要的?
(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
与字符串(from "foo"
)统一,又与数字(from x
)统一,所以出现类型不匹配。
注意
(define f : (('a -> 'a) (listof 'a) -> string)
(lambda (func lst) "hello"))
和
(define (f [func : ('a -> 'a)] [lst : (listof 'a)]) : string
"hello")
是不同的。在前者中,'a
指的是跨参数的相同类型变量。在后者中,func
's'a
和lst
's'a
是不同的。因此,在后者中,以下表达式类型检查:
(f (lambda ([x : number]) x) (list "foo"))