2015-05-04 155 views
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語句的工作,但必須有一個更好的辦法...

+5

無論是我錯了,你就用一些非常晦澀難懂的語法;或者,這甚至不是遠程的haskell語法(最好你可以通過設置多行選項將它傳遞給ghci)。問題不在於這裏有什麼問題;相反,只有很少的東西與此相關。你可能想再次看一下初學者的介紹,並用更小的工​​作代碼來構建這個開始。 – Cubic

回答