Haskell - 输出一个带2个参数的类型

时间:2016-10-10 20:38:52

标签: haskell

我正在尝试以[('letter',occurrence),(),...]的形式创建我自己的letterCount类型的数组。如何输出一个带有2个参数的类型。这是我的代码:

type LetterCount = (Char,Int)

 letterOccur :: Char->[Char] -> Int
 letterOccur c [] = 0
 letterOccur c (x:xs) = if (c == x) then ((letterOccur c xs) + 1)
                          else letterOccur c xs

 letterStats :: [Char] -> [LetterCount]
 letterStats :: [] = []
 letterStats (x:xs) = [x,(letterOccur x (x:xs))] ++ letterStats xs

1 个答案:

答案 0 :(得分:0)

我猜你是否正在尝试做这个之类的事情:

letterStats :: [Char] -> [LetterCount]
letterStats :: [] = []
letterStats (x:xs) = (x, (+1) $ letterOccur x xs) : letterStats xs

您要做的只是添加+1 ,因为在检查所有x次出现的x时,您不计算x。您还想要的是返回 letterCount类型的列表,其中是元组而不是列表,因此我更改了

[x, letterOccur x xs] ++ letterStats xs

(x, (+1) $ letterOccur x xs) : letterStats xs

虽然你也可以这样做:

[(x, (+1) $ letterOccur x xs)] ++ letterStats xs

但是没必要。