Jump to content

Recommended Posts

Posted

Olá amigos alguém é capaz de me dizer como é que eu obtenho o seguinte resultado numa lista por compreensao, ou seja quero fazer uma lista por compreensao cujo resultado final seja o seguinte :

[[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5]]

Se alguém souber agradeço.

Posted

Ah, obrigado eu não tinha pensado nisso, já tinha feito usando a recursividade, mas o exercicio era mesmo por compreensao, obrigado.

Já agora aqui fica usando recursividade:

geraLista :: [int]-> [[int]]
geraLista [] = []
geraLista l = geraLista (init l) ++ [elementosAte (length l) l]

elementosAte :: Int -> [int] -> [int]
elementosAte m (h:t) | (m == 1) = [h]
                     | otherwise = [h] ++ elementosAte (m-1) t

Um abraço atodos

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.