Recita a cantiga infantil «This is the House that Jack Built».
O processo de colocar uma frase ou oração dentro de outra frase ou oração chama-se encaixe. É através dos processos de recursão e de encaixe que conseguimos pegar num número finito de formas (palavras e frases) e construir um número infinito de expressões. Além disso, o encaixe permite também construir uma estrutura infinitamente longa, pelo menos em teoria.
A cantiga infantil diz o seguinte:
This is the house that Jack built.
This is the malt
that lay in the house that Jack built.
This is the rat
that ate the malt
that lay in the house that Jack built.
This is the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the farmer sowing his corn
that kept the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the horse and the hound and the horn
that belonged to the farmer sowing his corn
that kept the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
Este exercício é sobre refatoração de código, por isso fornecemos-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:
Sem pressa.
Muda uma coisa de cada vez e verifica se a tua solução continua a passar nos testes.
Diverte-te!
Inscreve-te no Exercism para aprenderes e dominares Haskell com 107 exercícios, e mentoria humana real, tudo grátis.