gdj*_*gdj 6 haskell function-composition
我试图用类型(Floating a) => a -> a -> a的函数组成一个类型的函数(Floating a) => a -> a来获得类型的函数(Floating a) => a -> a -> a.我有以下代码:
test1 :: (Floating a) => a -> a -> a
test1 x y = x
test2 :: (Floating a) => a -> a
test2 x = x
testBoth :: (Floating a) => a -> a -> a
testBoth = test2 . test1
--testBoth x y = test2 (test1 x y)
Run Code Online (Sandbox Code Playgroud)
但是,当我在GHCI中编译它时,我收到以下错误:
/path/test.hs:8:11:
Could not deduce (Floating (a -> a)) from the context (Floating a)
arising from a use of `test2'
at /path/test.hs:8:11-15
Possible fix:
add (Floating (a -> a)) to the context of
the type signature for `testBoth'
or add an instance declaration for (Floating (a -> a))
In the first argument of `(.)', namely `test2'
In the expression: test2 . test1
In the definition of `testBoth': testBoth = test2 . test1
Failed, modules loaded: none.
Run Code Online (Sandbox Code Playgroud)
请注意testBoth编译的编译版本.奇怪的是,如果我删除了(Floating a)所有类型的签名,或者限制我改变test1只取x而不是x和y,testBoth编译.
我搜索过StackOverflow,Haskell wikis,Google等,但没有发现任何与此特定情况相关的功能组成限制.有谁知道为什么会这样?
eph*_*ent 15
\x y -> test2 (test1 x y)
== \x y -> test2 ((test1 x) y)
== \x y -> (test2 . (test1 x)) y
== \x -> test2 . (test1 x)
== \x -> (test2 .) (test1 x)
== \x -> ((test2 .) . test1) x
== (test2 .) . test1
Run Code Online (Sandbox Code Playgroud)
这两件事情彼此并不相同.
test2 . test1
== \x -> (test2 . test1) x
== \x -> test2 (test1 x)
== \x y -> (test2 (test1 x)) y
== \x y -> test2 (test1 x) y
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1100 次 |
| 最近记录: |