La casa

La casa

Medio

Istruzioni

Recita la filastrocca «This is the House that Jack Built».

Il processo di collocare una frase o una proposizione dentro un'altra frase o proposizione si chiama annidamento. È grazie ai processi di ricorsione e annidamento che riusciamo a prendere un numero finito di forme (parole e frasi) e a costruire un numero infinito di espressioni. Inoltre, l'annidamento ci permette anche di costruire una struttura infinitamente lunga, almeno in teoria.

La filastrocca recita così:

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.

Suggerimenti

Questo esercizio riguarda la riorganizzazione del codice, perciò ti forniamo una soluzione che supera già i test.

La sfida è riscriverla finché non ne sei orgoglioso, imparando qualcosa nel frattempo.

Se non sai da dove iniziare, ecco qualche idea:

  • Prova a ridurre al minimo le ripetizioni.
  • Prova a rendere il codice leggibile.

Prenditi il tuo tempo.

Cambia una cosa alla volta e controlla se la soluzione supera ancora i test.

Divertiti!

Modifica tramite GitHub Il link si apre in una nuova finestra o scheda
Haskell Exercism

Vuoi iniziare La casa?

Iscriviti a Exercism per imparare e padroneggiare Haskell con 107 esercizi e il mentoring di persone reali, tutto gratis.