Percursos
/
Haskell
Haskell
/
Exercícios
/
Cadeia Alimentar
Cadeia Alimentar

Cadeia Alimentar

Médio

Instruções

Gera a letra da canção «I Know an Old Lady Who Swallowed a Fly».

Embora possas copiar e colar a letra, ou lê-la de um ficheiro, este problema é muito mais interessante se o abordares de forma algorítmica.

Esta é uma canção cumulativa de origem desconhecida.

Esta é uma de muitas variantes comuns.

I know an old lady who swallowed a fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a spider.
It wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a bird.
How absurd to swallow a bird!
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a cat.
Imagine that, to swallow a cat!
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a dog.
What a hog, to swallow a dog!
She swallowed the dog to catch the cat.
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a goat.
Just opened her throat and swallowed a goat!
She swallowed the goat to catch the dog.
She swallowed the dog to catch the cat.
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a cow.
I don't know how she swallowed a cow!
She swallowed the cow to catch the goat.
She swallowed the goat to catch the dog.
She swallowed the dog to catch the cat.
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a horse.
She's dead, of course!

Dicas

Este exercício é sobre refactorização de código, por isso damos-te uma solução que já passa nos testes.

O desafio é reescrevê-la até teres orgulho nela e aprenderes algo pelo caminho.

Se não sabes por onde começar, aqui ficam algumas ideias:

  • Tenta reduzir a repetição ao mínimo.
  • Tenta tornar o código legível.
  • Generaliza a tua solução, permitindo que a senhora engula outras coisas.

Leva o tempo que precisares.

Altera uma coisa de cada vez e verifica se a tua solução continua a passar nos testes.

Diverte-te!


Fonte

WikipediaO link abre numa nova janela ou separador
Editar via GitHub A ligação abre numa nova janela ou separador
Haskell Exercism

Estás pronto para começar Cadeia Alimentar?

Inscreve-te no Exercism para aprenderes e dominares Haskell com 107 exercícios, e mentoria humana real, tudo grátis.