2012-05-24 40 views
1

您好我能不知道這一點我自己,我也不能在網上找到一個也許,只是更多然後3個變量

我試圖用一個也許一個例子或保護和實例我發現只有兩個變量, 當我編輯或下面的例子中有兩個以上我得到下面有一個錯誤就是我試圖做

--maybe example 
    Fun :: Double -> Double -> Double -> Maybe Double 
    Fun a b c 
     | a >= -1.0 || a <= 1.0 || b >= -1.0 || b <=1.0 || c /=0 || c >= -1.0 = Nothing 
     | otherwise = Just max( ((c^ (-c)) + (a^(-c)-1.0)^ (a+ (-1.0/a)))  0.0) 

誤差GCHI

The function `Just' is applied to two arguments, 
    but its type `a0 -> Maybe a0' has only one 

情況H lint會給出

No suggestions 

試圖用一個後衛,而不是我得到一個不同的錯誤

--guard example 
    Fun :: Double -> Double -> Double -> Maybe Double 
    Fun a b c 
     | a >= -1.0 || a <= 1.0 || b >= -1.0 || b <=1.0 || c /=0 || c >= -1.0 = error "Value out of range " 
     | otherwise = max( ((c^ (-c)) + (a^(-c)-1.0)^ (a+ (-1.0/a)))  0.0) 

和繼承人GHC和hlints錯誤

test.hs:10:1: 
     Invalid type signature: Fun :: Double 
             -> Double -> Double -> Maybe Double 
     Should be of form <variable> :: <type> 
    $ hlint test.hs 
Error message: 
    Left-hand side of type signature is not a variable: Fun 
Code: 
    Fun :: Double -> Double -> Double -> Maybe Double 
    > Fun a b c 
+0

'樂趣'應該是一個函數,'樂趣',不是? –

回答

7

您必須編寫函數與小寫字母。這意味着寫此:

fun :: Double -> Double -> Double -> Maybe Double 
fun a b c ... 

函數可以從不與大寫字母開頭,由於大寫標識符被保留用於模塊(Data.List例如)和類型構造函數和數據構造(像IntMaybeJust)和其他一些事情。

您也有一個Just構造函數的應用程序的問題。如果你這樣寫:

Just max(something) 

...這意味着同樣的事情:

Just max something 

即你給Just兩個參數。您需要調整您的括號來解決這個問題:

Just (max something) 
3

語法錯誤,主要是:

f :: Double -> Double -> Double -> Maybe Double 
f a b c 
    | a >= -1 
    || a <= 1 
    || b >= -1 
    || b <= 1 
    || c /= 0 
    || c >= -1 
    = Nothing 

    | otherwise 
    = Just $ max ((c ** (-c)) + (a ** (-c)-1) ** (a+ (-1/a))) 0 

注意使用更普遍類型的exponention運營商,(**)$避免在括號Just包裝max

相關問題