2015-05-06 20 views
1
使用括號的

我收到此錯誤:在Haskell

<interactive>:145:29: 
    Could not deduce (Integral ([a0] -> Int)) 
     arising from a use of ‘fromIntegral’ 
    from the context (Num ([a] -> a), Fractional a) 
     bound by the inferred type of 
       meanList :: (Num ([a] -> a), Fractional a) => [a] -> a 
     at <interactive>:145:5-50 
    The type variable ‘a0’ is ambiguous 
    In the second argument of ‘(/)’, namely ‘(fromIntegral length x)’ 
    In the expression: (sum x)/(fromIntegral length x) 
    In an equation for ‘meanList’: 
     meanList x = (sum x)/(fromIntegral length x) 

由產生上述錯誤:

然而,當更新這:

let meanList x = sum x/fromIntegral (length x) 

然後,所有很好。

括號在Haskell中如何工作?

+1

這不是括號中的問題,而是函數應用。 – AJFarmar

+0

如果有疑問,請添加'$':'fromIntegral $ length x'與fromIntegral(length x)'相同,但不需要跟蹤右括號必須位於何處。 – 9000

回答

13

函數應用程序是關聯的。換句話說,

fromIntegral length x = (fromIntegral length) x 

因此,錯誤Could not deduce (Integral ([a0] -> Int))因爲長度的類型確實沒有的Integral一個實例。

+0

謝謝莎拉!你用什麼Haskell? –

+4

@Yuck你可以在freenode上抓到我,因爲我認爲這不是一個這樣的談話的相關場所。另外:一切。 :) – Sarah

+1

@Yuck多麼奇怪的問題:o – Carsten