Haskell:具有两个浮动参数的组合函数失败

编程入门 行业动态 更新时间:2024-10-21 19:50:48
本文介绍了Haskell:具有两个浮动参数的组合函数失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图编写一个类型为(Floating a)=>的函数。 a - > a - >一个带有类型的函数的(Floating a)=> a - > a 来获得类型为(Floating 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 - > testBoth = test2。 test1 --testBoth xy = test2(test1 xy)

然而,当我编译它时在GHCI中,我得到以下错误:

/path/test.hs:8:11:无法从/path/test.hs:8:11-15 $ b $处使用`test2'引起的上下文(Floating a)中的浮点数(a-> a) b可能的修正:将 add(浮动(a - > a))添加到的上下文中`testBoth'的类型签名或者添加一个实例声明(Floating(a - > ; a))在`(。)'的第一个参数中,即`test2'在表达式:test2中。 test1 在`testBoth'的定义中:testBoth = test2。 test1 失败,模块加载:无。

请注意,注释版本 testBoth 编译。奇怪的是,如果我从所有类型签名中删除(Floating a)约束,或者如果将 test1 更改为只需 x 而不是 x 和 y , testBoth 编译。

我搜索了StackOverflow,Haskell wiki,Google等,功能组成与这种特定情况有关。有人知道为什么会这样吗?

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

这两样东西并不相同。

test2。 test1 == \x - > (test2。test1)x == \x - > test2(test1 x) == \x y - > (test2(test1 x))y == \x y - > test2(test1 x)y

I am trying to compose a function of type (Floating a) => a -> a -> a with a function of type (Floating a) => a -> a to obtain a function of type (Floating a) => a -> a -> a. I have the following code:

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)

However, when I compile it in GHCI, I get the following error:

/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.

Note that the commented-out version of testBoth compiles. The strange thing is that if I remove the (Floating a) constraints from all type signatures or if I change test1 to just take x instead of x and y, testBoth compiles.

I've searched StackOverflow, Haskell wikis, Google, etc. and not found anything about a restriction on function composition relevant to this particular situation. Does anyone know why this is happening?

解决方案

\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

These two things are not like each other.

test2 . test1 == \x -> (test2 . test1) x == \x -> test2 (test1 x) == \x y -> (test2 (test1 x)) y == \x y -> test2 (test1 x) y

更多推荐

Haskell:具有两个浮动参数的组合函数失败

本文发布于:2023-10-26 23:59:57,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   函数   参数   两个   Haskell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!