Διαδρομές
/
Haskell
Haskell
/
Ασκήσεις
/
Τροφική Αλυσίδα
Τροφική Αλυσίδα

Τροφική Αλυσίδα

Μέτριο

Οδηγίες

Δημιούργησε τους στίχους του τραγουδιού "I Know an Old Lady Who Swallowed a Fly".

Ενώ θα μπορούσες να αντιγράψεις/επικολλήσεις τους στίχους, ή να τους διαβάσεις από ένα αρχείο, αυτό το πρόβλημα είναι πολύ πιο ενδιαφέρον αν το προσεγγίσεις αλγοριθμικά.

Πρόκειται για ένα σωρευτικό τραγούδι άγνωστης προέλευσης.

Αυτή είναι μία από τις πολλές κοινές παραλλαγές.

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!

Υποδείξεις

Αυτή η άσκηση αφορά την αναδιαμόρφωση κώδικα, γι' αυτό σου δίνουμε μια λύση που περνάει ήδη τα tests.

Η πρόκληση είναι να τη γράψεις ξανά μέχρι να είσαι περήφανος γι' αυτήν, και να μάθεις κάτι στην πορεία.

Αν δεν ξέρεις από πού να ξεκινήσεις, ορίστε μερικές ιδέες:

  • Προσπάθησε να περιορίσεις την επανάληψη στο ελάχιστο.
  • Προσπάθησε να κάνεις τον κώδικα ευανάγνωστο.
  • Γενίκευσε τη λύση σου, ώστε η γριά να μπορεί να καταπιεί κι άλλα πράγματα.

Πάρε τον χρόνο σου.

Άλλαζε ένα πράγμα τη φορά και έλεγχε αν η λύση σου περνάει ακόμα τα tests.

Καλή διασκέδαση!


Πηγή

WikipediaΟ σύνδεσμος ανοίγει σε νέο παράθυρο ή καρτέλα
Επεξεργασία μέσω GitHub Ο σύνδεσμος ανοίγει σε νέο παράθυρο ή καρτέλα
Haskell Exercism

Έτοιμος να ξεκινήσεις την άσκηση Τροφική Αλυσίδα;

Γράψου στο Exercism για να μάθεις και να κατακτήσεις Haskell με 107 ασκήσεις και πραγματική καθοδήγηση από ανθρώπους, όλα δωρεάν.