Jump to content
Sign in to follow this  
dardevil

Strings

Recommended Posts

dardevil

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.

Share this post


Link to post
Share on other sites
jpedro20

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.

Share this post


Link to post
Share on other sites
motherFFH

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.

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
Sign in to follow this  

×
×
  • 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.