Haskell在另一个函数中使用多个函数

Haskell在另一个函数中使用多个函数

本文介绍了Haskell在另一个函数中使用多个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个程序来计算haskell中三次方程的解.我是功能语言的新手,但遇到了一些困难.这是我的代码:

I'm creating a program to compute the solution to a cubic equation in haskell. I'm new to functional languages and I'm having some difficulties. Here is my code:

cubicQ :: Float -> Float -> Float -> Float
cubicQ a b c = ((3 * a * c) - (b**2)) / (9 * (a**2)) --calculates Q

cubicR :: Float -> Float -> Float -> Float -> Float
cubicR a b c d = ((9 * a * b * c) - (27 * a**2 * d) - (2 * b**3)) / (54
* a**3)--calculates R

cubicS :: Float -> Float -> Float
cubicS r q = (r + (sqrt(q**3 + r**2))) ** (1/3)--calculates S

cubicT :: Float -> Float -> Float
cubicT q r = (r - (sqrt(q**3 + r**2))) ** (1/3)--calculates T

check :: Float -> Float -> Float
check q r = ((q**3)+(r**2)) --checks for real  numbers

x1 :: Float -> Float -> Float -> Float -> Float
x1 s t a b = (s + t) - (b / (3 * a)) --calculates x1

cubicRealSolution :: Float -> Float -> Float -> Float -> Float
--defines function which takes input and solves
cubicRealSolution a b c d = if l < 0 then error "NaN" else (sol) --Check
for real numbers
                where
                  q = cubicQ
                  r = cubicR
                  s = cubicS
                  t = cubicT
                  l = check
                  sol = x1

编译时出现此错误,* Couldn't match expected type Float' with actual type Float -> Float -> Float -> Float -> Float' * Probable cause: sol' is applied to too few arguments我不确定从这里要去哪里.如果我删除所有功能,而只是使变量等于thr函数所做的计算,就可以使它正常工作,但这是为学校分配的作业,因此必须定义这些功能.

I get this error when compiling, * Couldn't match expected type Float' with actual type Float -> Float -> Float -> Float -> Float' * Probable cause: sol' is applied to too few argumentsI'm not sure where to go from here. I can get it to work if i remove all of the functions and instead just make my variables equal to the computations that thr functions do, but it's for a school assignment and so I have to have these functions defined.

推荐答案

在您的cubicRealSolution中,您编写:

where q = cubicQ

(还有其他一些声明,但是所有这些声明都存在相同的问题).

(some other declarations as well, but these all suffer from the same problem).

此问题是您现在定义类型为q :: Float -> Float -> Float -> Float的函数q.因此,您进行函数调用,只需声明q一个函数即可.

The problem with this is that you now define a function q with type q :: Float -> Float -> Float -> Float. You thus do not make a function call, you simply declare q a function.

您可以使用cubicQ a b c调用该函数(其中abc是在cubicRealSolution head 中定义的de标识符,请勿与cubicQ函数).

You call the function with cubicQ a b c (where a, b and c are de identifiers defined in the head of the cubicRealSolution, not to be confused with the cubicQ function).

因此至少要进行类型检查的函数应如下所示:

So a function that at least would typecheck could look like:

cubicRealSolution :: Float -> Float -> Float -> Float -> Float
cubicRealSolution a b c d = if l < 0 then error "NaN" else sol
    where q = cubicQ a b c
          r = cubicR a b c d
          s = cubicS r q
          t = cubicT q r
          l = check q r
          sol = x1 s t a b

因此,

Haskell不会查找具有相同名称的标识符,然后使用此参数调用该函数.在我看来,这样的按名称检索"将是非常不安全的,因为添加一个额外的参数(或重命名一个参数)可能会产生各种难以预料的影响.实际上,参数的名称通常并不重要(有一些值得注意的例外,例如在 Haskell模板中编写部件).

Haskell thus does not look for identifiers with the same name, and then calls the function with this parameters. Such "retrieval by name" would, in my opionion, be very unsafe, since adding an extra parameter (or renaming one) could have all sorts of effects that are very hard to predict. In fact the name of the parameter usually does not matter much (with some noteworthy exceptions like writing parts in template Haskell).

但是,如果cubicQ旨在计算三次多项式的根,则似乎返回了错误的结果.因此,仍然存在语义上的问题.

If cubicQ however aims to calculate the roots of a cubical polynomial, then it seems to return the wrong result. So there is still a semantical problem.

这篇关于Haskell在另一个函数中使用多个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 16:02