0
作为学习Haskell的练习,我决定尝试翻译一些用C语言编写的作业,使用GSL库。作为我绝对的初学者,我正在绊倒很多事情。Haskell缩进 - if语句
我不确定以下是缩进错误还是我没有正确使用if语句,而Google没有任何帮助。我的目标是编写正态分布的累积分布函数。 (见编辑用于精简版)
下面是代码:
import Bindings.Gsl.MathematicalFunctions
cdf_normal_as :: x -> fx (Double, Double)
let {
fx = 0;
psi_x = 0;
y = 0;
b = 0.2316419;
a1 = 0.319381530;
a2 = (-0.356563782);
a3 = 1.781477937;
a4 = (-1.821255978);
a5 = 1.330274429;
}
if x >= 0
then
let
y = 1/(1 + b * x);
psi_x = (1/(sqrt(2 * M_PI))) * exp (-(pow(x,2)/2));
in
fx = 1.0 - psi_x * (a1 * y + a2 * y * y + a3 * y * y * y + a4 * y * y * y * y + a5 * y * y * y * y * y);
else
let
x = x * (-1);
y = 1/(1 + (b * x));
psi_x = (1/(sqrt(2 * M_PI))) * exp(-(pow(x,2)/2));
in
fx = 1 - psi_x * (a1*y + a2*pow(y,2) + a3*pow(y,3) + a4*pow(y,4) + a5*pow(y,5));
cdf_normal_as fx;
这里是我得到的消息:
Prelude> :l Normal_CDF.hs
[1 of 1] Compiling Main (Normal_CDF.hs, interpreted)
Normal_CDF.hs:20:1:
parse error (possibly incorrect indentation or mismatched brackets)
Failed, modules loaded: none.
Prelude>
我在做什么错?
---------------------------------------编辑
我做一个更精简的版本,以便更好地了解可能存在的问题。 我的问题是:在Haskell中做相当于if-else语句的正确和最好的方法是什么?
下面的代码应该在y变量中设置某个变量x为一个常量,然后在执行特定任务之前检查y是大于还是小于0,如果是或者不是。这编译。
plus_or_double :: Double -> Double
plus_or_double y = let {
x = 2;
} in case (y>= 0) of
True -> (x + y)
False -> y
我用case语句,因为我无法让if语句的工作,但必须有一个更好的办法...
无论是我错了,你就用一些非常晦涩难懂的语法;或者,这甚至不是远程的haskell语法(最好你可以通过设置多行选项将它传递给ghci)。问题不在于这里有什么问题;相反,只有很少的东西与此相关。你可能想再次看一下初学者的介绍,并用更小的工作代码来构建这个开始。 – Cubic