Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Haskell计算字符串中元音和辅音的数量?_Haskell - Fatal编程技术网

如何使用Haskell计算字符串中元音和辅音的数量?

如何使用Haskell计算字符串中元音和辅音的数量?,haskell,Haskell,我开始学习哈斯克尔,我完全不知所措。这个练习要求我计算一个字符串中元音和辅音的数量,并打印这两个数字 以下是我目前掌握的代码: --Here I take the string and will --return a tuple with both values countVC::[Char]->(Int, Int) --I call an aux function where I pass the string --and two values, which I will use t

我开始学习哈斯克尔,我完全不知所措。这个练习要求我计算一个字符串中元音和辅音的数量,并打印这两个数字

以下是我目前掌握的代码:

--Here I take the string and will 
--return a tuple with both values
countVC::[Char]->(Int, Int)
--I call an aux function where I pass the string 
--and two values, which I will use to increment
--according to the amount of vowels or consonants 
countVC = countVCAux txt 0 0

countVCAux::[Char]->Int->Int->(Int, Int)
--If the string is empty I try to return the tuple with (0, 0)
countVCAux [] con vow  = (con, vow)
--If not I take the head and compare with the consonants
countVCAux (c:r) con vow
    --If it's a vowel I pass the rest of the list, the consonant and increment the vowel count
    |c=='a' || c=='e' || c=='i' || c=='o' || c=='u' = countVCAux r con (vow + 1)
    --Else I do the same, but increment the consonant count
    |otherwise = countVCAux r (con + 1) vow

但是它不起作用。此代码有什么问题?

一个问题是您的
countVC
定义当前没有按照其签名所示采用
[Char]
参数。将其更改为:

countVC txt=countVCAux txt 0
第一个
countVCAux
模式也不太正确
txt
可能应该省略,以支持空字符串
[]
,您需要添加
con
vow
参数:

countVCAux[]con-vow=(con,vow)

什么不起作用?存在类型错误。首先,你必须修复它们。已经做了Chad Gilbert建议的修复并更新了原始帖子。我现在得到一个错误“未定义变量'txt'”。谢谢。我修正了第一个countVCAux模式。然而,我不明白我应该如何更改countVC定义,因为您文章中的代码已经与我原始文章中的同一行代码匹配。我现在得到一个错误“未定义变量'txt'”。呜呜!我刚刚修正了我的答案。第一个示例现在包括
txt
作为参数。非常感谢您!现在工作得很好D