假设我有以下功能:
--count number of an item in a list
count :: (Eq a) => a -> [a] -> Double
count x [] = 0.0
count x (y:ys) = (is x y) + count x ys
和
--returns 1 if items match, else 0
is :: (Eq a) => a -> a -> Double
is x y
| x == y = 1.0
| otherwise = 0.0
和
--compute length of the list
len :: [a] -> Double
len [] = 0.0
len [x] = 1.0
len (x:xs) = 1.0 + len xs
我想用这个方法来生成一个生成标准化计数的函数:
--generates frequency of item in list
ncount :: (Eq a) => a -> [a] -> Double
ncount x [] = 0.0
ncount x y = norm * (count x y)
where
norm = 1.0 / len y
我只是想知道在这种情况下应该如何处理签名。count
有签名(Eq a) => a -> [a] -> Double
,但也应该ncount
有吗?一方面如果调用时a
不在,后续调用会失败。另一方面,从不测试平等。Eq
ncount
count
ncount
对不起,被遗漏了is
和len
.:w