相关疑难解决方法(0)

Haskell组成有两个参数

我试图通过Haskell理解函数式编程,并且我在处理函数组合时遇到了很多麻烦.

其实我有这两个功能:

add:: Integer -> Integer -> Integer
add x y =  x + y

sub:: Integer -> Integer -> Integer
sub x y =  x - y
Run Code Online (Sandbox Code Playgroud)

我希望能够撰写它们.它没有任何意义,但这是一个学习目标.

我尝试过的:

foo:: (Integer -> Integer) -> (Integer -> Integer) -> Integer
foo = add . sub
Run Code Online (Sandbox Code Playgroud)

我明白了:

Haskell使用只有一个args的函数,因此我们返回一个新函数,在每次执行函数后执行.

所以第一个Integer是param类型,而第二个是生成函数的返回类型,必须添加第二个数字.

这将返回另一个函数(sub),它将产生相同的流程(返回带有参数等的函数...)

我对吗 ?

这是我的实际错误代码:

src\Main.hs:23:7:
    Couldn't match type `Integer' with `Integer -> Integer'
    Expected type: Integer -> (Integer -> Integer) -> Integer
      Actual type: Integer -> Integer -> …
Run Code Online (Sandbox Code Playgroud)

haskell functional-programming function-composition

7
推荐指数
2
解决办法
780
查看次数