轨道
/
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!

提示

这个练习的主题是代码重构,所以我们为你准备了一份已经通过测试的解答。

挑战在于把它重写到你引以为傲为止,并在这个过程中学到一些东西。

如果你不知道从哪里开始,这里有一些思路:

  • 尽量把重复减少到最低。
  • 尽量让代码更易读。
  • 把解答泛化,让老太太能吞下别的东西。

慢慢来。

每次只改一处,然后检查你的解答是否仍然能通过测试。

祝你玩得开心!


来源

Wikipedia链接会在新窗口或新标签页中打开
通过 GitHub 编辑 链接将在新窗口或新标签页中打开
Haskell Exercism

准备好开始 食物链 了吗?

注册 Exercism,借助 107 个练习 和真人导师指导,学习并掌握 Haskell,全部免费。