2016-12-12 8 views
-1

私はハスケルを勉強し始めています。私は完全に迷っています。この演習では、母音と母音の文字列の数を数え、両方の数字を印刷する必要があります。それは動作しません。しかし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 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 

:ここ

は、私がこれまで持っているコードです。このコードの問題は何ですか?

+3

を?タイプエラーがあります。まず第一に、それらを修正する必要があります。 – freestyle

+0

Chad Gilbertが提案した修正をすでに元のポストに更新しました。私は今、 "Undefined variable 'txt'"というエラーを受け取ります。 – artie

答えて

1

1つの問題は、countVCの定義では現在、そのシグネチャが意味するように[Char]パラメータが使用されないことです。このように変更します。

countVC txt = countVCAux txt 0 0 

最初​​パターンは、いずれかの非常に適切ではありません。 txtはおそらく、空の文字列[]の賛成では省略されなければならない、とあなたはconvowパラメータを追加する必要があります。正確に動作しません何

countVCAux [] con vow = (con, vow) 
+0

ありがとうございます。最初のcountVCAuxパターンを修正しました。しかし、あなたの投稿のコードが元の投稿のコードと同じ行に既に一致しているので、私はcountVC定義をどのように変更する必要があるのか​​分かりません。私は今、 "Undefined variable 'txt'"というエラーを受け取ります。 – artie

+0

Woops!私はちょうど私の答えを修正した。最初の例では、パラメータとして 'txt'が含まれています –

+0

ありがとうチャド!素晴らしい今作業! :D – artie

関連する問題