dardevil Posted July 10, 2012 at 03:20 PM Report #468201 Posted July 10, 2012 at 03:20 PM Boas pessoal!! Queria que me ajudassem aqui neste problema. Tenho este exemplo: superStrings :: [string] -> [string] -> [(String, Int)] superStrings ["mar", "ter"] ["termos", "maré", "ovo", "amar", "bater", "amarelo", "loja", "marujo"] resulta em [("mar", 4), ("ter", 2)] ou seja a função vai contar quantas vezes aparece "mar" e "ter" na lista de Strings dada. Já tenho isto feito, mas não estou a conseguir sair daqui: superStrings :: [string] -> [string] -> [(String, Int)] superStrings [] [] = [] superStrings _ [] = [] superStrings [] _ = [] superStrings (x:ys) (z:zs) |x == z = (x, aux x z) |otherwise = (ys, aux ys zs) aux :: [string] -> [string] -> Int aux [] [] = 0 aux _ [] = 0 aux [] _ = 0 aux (x:xs) (z:zs) |x == z = 1 + aux x zs |otherwise = 1 + aux xs zs Agradecia se me pudessem ajudar.
jpedro20 Posted July 10, 2012 at 04:29 PM Report #468217 Posted July 10, 2012 at 04:29 PM Repara aqui: superStrings (x:ys) (z:zs) |x == z = (x, aux x z) estás a fazer aux x z. x e z são Strings e a função aux recebe duas listas de Strings.
motherFFH Posted July 16, 2012 at 07:53 PM Report #468936 Posted July 16, 2012 at 07:53 PM Pensa em termos funcionais: tens duas listas e queres contar os elementos da segunda que contêm a substring de cada elemento da primeira. import Data.List superStrings xs ys = [ (x, length $ filter (isInfixOf x) ys) | x <- xs ] A tua "solução" é uma tentativa de programação imperativa através de recursão.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now