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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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.