相关疑难解决方法(0)

如何在内部类型声明中重用类型变量

作为我的Haskell学习过程的一部分,我喜欢明确地输出函数的类型声明.我希望能够为where子句中定义的函数执行此操作,但我不知道如何指定,where子句中的类型变量应该表示与外部类型声明中的某个类型变量相同的类型.例如,以下代码:

foo :: (a -> a) -> a -> a
foo f arg = bar arg
  where
    bar :: a -> a
    bar a = f a
Run Code Online (Sandbox Code Playgroud)

产生此错误:

src\Test.hs:7:14:
    Couldn't match expected type `a' against inferred type `a1'
      `a' is a rigid type variable bound by
          the type signature for `foo' at src\Test.hs:3:8
      `a1' is a rigid type variable bound by
           the type signature for `bar' at src\Test.hs:6:11
    In the first argument of `f', namely `a'
    In the expression: f …
Run Code Online (Sandbox Code Playgroud)

haskell

12
推荐指数
2
解决办法
1057
查看次数

标签 统计

haskell ×1