我正在编写一个函数,它接受一个函数和一个列表作为参数。参数函数和列表必须具有相同类型的值。我该怎么保证呢?
我试过:
(define ( (func -> 'a) [lst : (Typeof 'a)])
....)然而,我一直未能使它发挥作用。我也读过编曲教程,但没有找到任何相关的东西。
是否可能有一个接受特定返回类型函数的函数?
发布于 2019-01-25 03:19:39
这就是你要找的吗?
(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与字符串(来自"foo")是统一的,但与数字(来自x)也是统一的,因此会出现类型不匹配。
请注意,
(define f : (('a -> 'a) (listof 'a) -> string)
(lambda (func lst) "hello"))和
(define (f [func : ('a -> 'a)] [lst : (listof 'a)]) : string
"hello")是不一样的。在前者中,'a跨参数引用相同类型的变量。在后者中,func的'a和lst的'a是不同的。因此,在后者中,下面的表达式类型检查:
(f (lambda ([x : number]) x) (list "foo"))https://stackoverflow.com/questions/54358167
复制相似问题