دوازده روز

دوازده روز

آسان

دستورالعمل‌ها

وظیفه‌ی شما در این تمرین این است که کدی بنویسید که متن ترانه‌ی «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» را تولید می‌کند. اگر درباره‌ی نوع‌ها مطمئن نیستید می‌توانید از امضای ارائه‌شده استفاده کنید، اما نگذارید خلاقیت شما را محدود کند.

این تمرین با داده‌های متنی کار می‌کند. به دلایل تاریخی، نوع String در Haskell با [Char]، یعنی فهرستی از نویسه‌ها، هم‌معناست. برای پردازش کارآمدتر داده‌های متنی می‌توان از نوع Text استفاده کرد.

به‌عنوان یک گسترش اختیاری برای این تمرین، می‌توانید

  • درباره‌ی نوع‌های رشته در Haskell بخوانید.
  • عبارت - text را به فهرست وابستگی‌هایتان در package.yaml اضافه کنید.
  • Data.Text را به این شکل وارد کنید:
import qualified Data.Text as T
import           Data.Text (Text)
  • مثلاً recite :: Int -> Int -> [Text] را بنویسید و مثلاً با T.pack به ترکیب‌کننده‌های Data.Text ارجاع دهید.
  • مستندات Data.Text را ببینید.
  • همه‌ی موارد String را در TwelveDays.hs با Text جایگزین کنید، یعنی:
recite :: Int -> Int -> [Text]

این بخش کاملاً اختیاری است.


منبع

Wikipediaاین لینک در پنجره یا تب جدیدی باز می‌شود.
ویرایش از طریق GitHub این لینک در پنجره یا زبانه‌ی جدیدی باز می‌شود
Haskell Exercism

آماده‌اید دوازده روز را شروع کنید؟

در Exercism ثبت‌نام کنید تا Haskell را همراه با 107 تمرین و مربی‌گری انسانی واقعی یاد بگیرید و در آن استاد شوید، همه‌ی این‌ها رایگان.