المسارات
/
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'. يمكنك استخدام التوقيع المقدم إذا لم تكن متأكدًا من الأنواع، لكن لا تدعه يحدّ من إبداعك.

يتعامل هذا التمرين مع بيانات نصية. ولأسباب تاريخية، فإن نوع String في Haskell مرادف لـ [Char]، أي مصفوفة من المحارف. ولمعالجة البيانات النصية بكفاءة أكبر، يمكن استخدام نوع Text.

وكإضافة اختيارية إلى هذا التمرين، يمكنك

import qualified Data.Text as T
import           Data.Text (Text)
  • أن تكتب مثلًا recite :: Int -> Int -> [Text] وتشير إلى دوال Data.Text التركيبية باسم مثل T.pack.
  • أن تبحث عن التوثيق الخاص بـ Data.Text.
  • أن تستبدل كل حالات String بـ Text في TwelveDays.hs، أي:
recite :: Int -> Int -> [Text]

هذا الجزء اختياري تمامًا.


المصدر

ويكيبيديايفتح الرابط في نافذة أو علامة تبويب جديدة
تعديل عبر GitHub يفتح الرابط في نافذة أو علامة تبويب جديدة
Haskell Exercism

مستعد لبدء الأيام الاثنا عشر؟

سجّل في Exercism لتتعلّم وتتقن Haskell عبر 107 تمارين، وإرشاد بشري حقيقي، وكل ذلك مجانًا.