十二天

十二天

簡單

說明

這個練習的任務是撰寫程式碼,回傳歌曲「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 型別。

作為這個練習的選用延伸,你可以

  • 閱讀 Haskell 的字串型別。
  • 在 package.yaml 的相依清單中加入 - text。
  • 依照下列方式匯入 Data.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,全部免費。