Parcours
/
Haskell
Haskell
/
Exercices
/
Chaîne alimentaire
Chaîne alimentaire

Chaîne alimentaire

Moyen

Instructions

Génère les paroles de la chanson « I Know an Old Lady Who Swallowed a Fly ».

Même si on peut copier-coller les paroles, ou les lire depuis un fichier, ce problème est bien plus intéressant quand on l'aborde de façon algorithmique.

C'est une chanson cumulative d'origine inconnue.

C'est l'une des nombreuses variantes courantes.

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!

Indices

Cet exercice porte sur la réécriture de code, c'est pourquoi on te fournit une solution qui passe déjà les tests.

Le défi consiste à la réécrire jusqu'à ce que tu en sois fier, et à apprendre quelque chose au passage.

Si tu ne sais pas par où commencer, voici quelques idées :

  • Essaie de réduire les répétitions au minimum.
  • Essaie de rendre le code lisible.
  • Généralise ta solution, pour permettre à la dame d'avaler d'autres choses.

Prends ton temps.

Modifie une chose à la fois et vérifie que ta solution passe toujours les tests.

Amuse-toi bien !


Source

WikipediaLe lien s'ouvre dans une nouvelle fenêtre ou un nouvel onglet
Modifie via GitHub Le lien s'ouvre dans une nouvelle fenêtre ou un nouvel onglet
Haskell Exercism

Prêt à commencer Chaîne alimentaire ?

Inscris-toi sur Exercism pour apprendre et maîtriser Haskell avec 107 exercices, et un vrai mentorat humain, le tout gratuitement.