简体   繁体   English

Haskell类型签名错误

[英]Haskell Type Signature Error

My function does not work. 我的功能不起作用。 I tried many different Type signatures. 我尝试了很多不同的Type签名。 If I remove the Type Signature it does not work with a point number as "p". 如果我删除了类型签名,则它不能用点号作为“p”。

fak :: (Num a, Ord a) => a->a
fak x
    | x <= 1 = 1
    | otherwise = x*fak (x-1)

ncr :: Integral a => a -> a -> a
ncr n k = (fak n) `div` (fak(n-k) * fak k)

bTable :: (Integral a, Num b) => a->b->a->a 
bTable n p k = (ncr n k) * p^k * (1-p)^(n-k)

Inferred type is not general enough 推断类型不够通用

*** Expression    : bTable
*** Expected type : (Integral a, Num b) => a -> b -> a -> a
*** Inferred type : (Integral a, Num a) => a -> a -> a -> a

If I remove the Type Signature I get: 如果我删除Type Signature,我会得到:

:t bTable
bTable :: Integral a => a -> a -> a -> a

But If I enter: 但如果我进入:

bTable 50 0.8 10

I get 我明白了

Unresolved overloading
*** Type       : (Fractional a, Integral a) => a
*** Expression : bTable 50 0.8 10

Use fromIntegral to convert the return value of ncr to something you can multiply with Num a => a values. 使用fromIntegralncr的返回值转换为可以与Num a => a值相乘Num a => a值。

bTable n p k = fromIntegral (ncr n k) * p^k * (1-p)^(n-k)

Note the inferred type of this function is then 请注意,此函数的推断类型是

bTable :: (Num a, Integral b) => b -> a -> b -> a

which is slightly different from your attempted declared type (with constraints renamed for comparison to the type above) 这与您尝试的声明类型略有不同(重命名约束以与上面的类型进行比较)

bTable :: (Num a, Integral b) => b -> a -> b -> b 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM