Pattern matching with one, two or more arguments

Hello, I'm writing a little word game that consists of the following : between a consonant and a vowel, insert "AV" so "fabien" becomes "fAVabAVien". When i type : f "fabine", the result is ok ("fAVabAVinAVe") When i type : f "fabien", the result is fAVabAVie *** exception :prelude empty list Here's my program voyelles="aeiouy" consonnes="bcdfghjklmnpqrstvwxz" est_voyelle x = x `elem` voyelles est_consonne x = x `elem` consonnes --f []=[] ; unuseful - conflict with f a = a f (a:b:c) = case trait a b of True -> a : (" AV " ++ f (b:c)) False -> a : f (b:c) f (a:b) = case trait a (head b) of True -> a : (" EP " ++ f ( b) ) False -> a : f ( b) f a = a -- detect a vowel following a consonant trait a b = est_consonne a && est_voyelle b f (a:b) is never invoked (i wrote EP instead of AV to trace the problem). f(a:b:c) seems to always take precedence over the other patterns. I expect f(a:b:c) to treat words greater than or equal to 3 letters, f(a:b) the last two letters of the word (no third part) and f a the last letter. Another trick : how can i calculate consonnes with a method that makes the difference between the two lists [a..z] and voyelles, rather than writing each consonant. As we can append two lists (++), can we compute the difference between lists, i.e. delete from the first list the elements that are present in the second one ? Last, with Ghci, is it possible to debug my program, especially for recursive functions, because it's hard to follow the successive calls and the parameters that are passed each turn. Greetings, Didier.

2009/11/29 legajid
Another trick : how can i calculate consonnes with a method that makes the difference between the two lists [a..z] and voyelles, rather than writing each consonant. As we can append two lists (++), can we compute the difference between lists, i.e. delete from the first list the elements that are present in the second one ?
Use Data.List.\\ ['a'..'z'] \\ "aoeui" ==> "bcdfghjklmnpqrstvwxyz" -- Deniz Dogan

On Sun, Nov 29, 2009 at 06:41:22PM +0100, legajid wrote:
Hello, I'm writing a little word game that consists of the following : between a consonant and a vowel, insert "AV" so "fabien" becomes "fAVabAVien".
When i type : f "fabine", the result is ok ("fAVabAVinAVe") When i type : f "fabien", the result is fAVabAVie *** exception :prelude empty list
Here's my program
voyelles="aeiouy" consonnes="bcdfghjklmnpqrstvwxz"
est_voyelle x = x `elem` voyelles est_consonne x = x `elem` consonnes
--f []=[] ; unuseful - conflict with f a = a
-- detect a vowel following a consonant trait a b = est_consonne a && est_voyelle b
f (a:b) is never invoked (i wrote EP instead of AV to trace the problem). f(a:b:c) seems to always take precedence over the other patterns. I expect f(a:b:c) to treat words greater than or equal to 3 letters, f(a:b) the last two letters of the word (no third part) and f a the last letter.
This is not correct. The patern (a:b:c) matches words greater than or equal to TWO letters, not three. In the case of a two-letter word, c is the empty list. For example, the word "in" is ('i':'n':[]).
f (a:b:c) = case trait a b of True -> a : (" AV " ++ f (b:c)) False -> a : f (b:c) f (a:b) = case trait a (head b) of True -> a : (" EP " ++ f ( b) ) False -> a : f ( b) f a = a
The problem is with the call to 'head b' -- when f is called on a one-letter word, it matches the pattern (a:b) with b the empty list, and the call to 'head' crashes. You do not need to check the trait in this case at all. In fact, you do not need this entire case at all! Just the first case and the third should suffice. -Brent

"legajid"
Hello, I'm writing a little word game that consists of the following : between a consonant and a vowel, insert "AV" so "fabien" becomes "fAVabAVien".
When i type : f "fabine", the result is ok ("fAVabAVinAVe") When i type : f "fabien", the result is fAVabAVie *** exception :prelude empty list
Here's my program
voyelles="aeiouy" consonnes="bcdfghjklmnpqrstvwxz"
est_voyelle x = x `elem` voyelles est_consonne x = x `elem` consonnes
--f []=[] ; unuseful - conflict with f a = a
f (a:b:c) = case trait a b of True -> a : (" AV " ++ f (b:c)) False -> a : f (b:c) f (a:b) = case trait a (head b) of True -> a : (" EP " ++ f ( b) ) False -> a : f ( b) f a = a
-- detect a vowel following a consonant trait a b = est_consonne a && est_voyelle b
f (a:b) is never invoked (i wrote EP instead of AV to trace the problem). f(a:b:c) seems to always take precedence over the other patterns. I expect f(a:b:c) to treat words greater than or equal to 3 letters, f(a:b) the last two letters of the word (no third part) and f a the last letter.
Another trick : how can i calculate consonnes with a method that makes the difference between the two lists [a..z] and voyelles, rather than writing each consonant. As we can append two lists (++), can we compute the difference between lists, i.e. delete from the first list the elements that are present in the second one ?
Last, with Ghci, is it possible to debug my program, especially for recursive functions, because it's hard to follow the successive calls and the parameters that are passed each turn.
import Char voyelles = "aeiouyAEIOUY" est_voyelle x = x `elem` voyelles est_consonne x = (isAlpha x) && (x `notElem` voyelles) f :: String -> String f (a:b:c) | trait a b = [a]++"AV"++[b]++(f c) | otherwise = a : f (b:c) f a = a trait a b = (est_consonne a) && (est_voyelle b)
participants (4)
-
Brent Yorgey
-
Deniz Dogan
-
legajid
-
Tim Attwood