轨道
/
Haskell
Haskell
/
练习
/
十二天
十二天

十二天

简单

说明

本次练习的任务是编写代码,返回歌曲“The Twelve Days of Christmas”的歌词。

“The Twelve Days of Christmas”是一首广为人知的英语圣诞颂歌。歌曲后面每一段歌词都建立在前一段的基础上。

你的代码返回的歌词必须与下面给出的完整歌词_完全_一致。

歌词

On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree.

On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree.

On the third day of Christmas my true love gave to me: three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the fourth day of Christmas my true love gave to me: four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the fifth day of Christmas my true love gave to me: five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the sixth day of Christmas my true love gave to me: six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the seventh day of Christmas my true love gave to me: seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the eighth day of Christmas my true love gave to me: eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the ninth day of Christmas my true love gave to me: nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the tenth day of Christmas my true love gave to me: ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the eleventh day of Christmas my true love gave to me: eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the twelfth day of Christmas my true love gave to me: twelve Drummers Drumming, eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

提示

你需要实现 recite 函数,它负责输出《The Twelve Days of Christmas》的歌词。如果对类型不太确定,可以使用提供的签名,但别让它限制你的创造力。

这一练习处理的是文本数据。由于历史原因,Haskell 的 String 类型等同于 [Char],也就是字符列表。为了更高效地处理文本数据,可以使用 Text 类型。

作为本练习的可选扩展,你还可以:

import qualified Data.Text as T
import           Data.Text (Text)
  • 例如写成 recite :: Int -> Int -> [Text],并用 T.pack 这样的方式引用 Data.Text 的组合子。
  • 查阅 Data.Text 的文档。
  • 把 TwelveDays.hs 中所有出现的 String 替换成 Text,即:
recite :: Int -> Int -> [Text]

这一部分完全是可选的。


来源

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

准备好开始 十二天 了吗?

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