食物鏈

食物鏈

中等

說明

產生歌曲「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!

提示

這個練習的主題是重構,所以我們提供了一份已經通過測試的解法。

挑戰在於把它改寫到你會為它感到驕傲,並在過程中學到一些東西。

如果你不知道該從哪裡開始,這裡有幾個想法:

  • 試著把重複的部分減到最低。
  • 試著讓程式碼更容易閱讀。
  • 把你的解法一般化,讓那位女士能吞下其他東西。

慢慢來吧。

一次只改一個地方,然後確認你的解法仍然能通過測試。

祝你玩得開心喔!


出處

Wikipedia連結會在新視窗或分頁中開啟
透過 GitHub 編輯 連結會在新視窗或分頁中開啟
Haskell Exercism

準備好開始 食物鏈 了嗎?

註冊 Exercism,透過 107 個練習 和真人引導來學習並精通 Haskell,全部免費。