Jump to content
Moises

Como fazer uma função duplicar fold em haskell

Recommended Posts

Moises

A função duplicarFold :: String -> String repete duas vezes cada vogal (letras 'a', 'e', 'i', 'o', 'u' minúsculas ou maiúsculas) numa cadeia de carateres; os outros carateres devem ficar inalterados. Por exemplo, 

duplicar "Ola, mundo!"== "OOlaa, muundoo!"

Da forma que fiz não esta funcionando:

duplicarFold :: String -> String 
duplicarFold xs = foldr (\x acc-> if elem x vogais then else ) [] xs
  where
  vogais = "aeiouAEIOU"

 


Moises 

Share this post


Link to post
Share on other sites
Baderous

Está praticamente feito, só te falta preencher o then e o else. No then apenas tens de repetir o x na String e juntar esse 2 caracteres ao acc.

duplicarFold :: String -> String
duplicarFold s = foldr (\x ss -> if isVowel x then x:x:ss else x:ss) [] s
        where isVowel x = elem x "aeiouAEIOU"

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.