Jump to content

Recommended Posts

Posted

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.

Posted

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.

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.