[Haskell-beginners] Re: Pattern matching with one, two or more arguments

Tim Attwood timothyea at comcast.net
Sun Nov 29 16:52:39 EST 2009


"legajid" <legajid at free.fr> wrote in message 
news:4B12B242.9050606 at free.fr...
> 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) 




More information about the Beginners mailing list