[Haskell-beginners] Pattern matching with one, two or more arguments
legajid at free.fr
Sun Nov 29 12:41:22 EST 2009
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
Here's my program
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
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.
More information about the Beginners