/* Twelve Days of Christmas using DCG in Picat. From https://www.reddit.com/r/prolog/comments/kip3wg/coding_challenge_27_2_weeks_the_twelve_days_of/ """ The task is to output some version of the lyrics of the cumulative song The Twelve Days of Christmas. """ This is a port of Markus Triska's DCG version. But it don't work in Picat, or rather: I have not been able to make it work in Picat. This model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import v3_utils. main => go. go ?=> lyrics(Ls,[]), println(Ls.flatten), nl. go => true. day(first) --> "A partridge in a pear tree.". day(second) --> "Two turtle doves". day(third) --> "Three french hens". day(fourth) --> "Four calling birds". day(fifth) --> "Five golden rings". day(sixth) --> "Six geese a-laying". day(seventh) --> "Seven swans a-swimming". day(eight) --> "Eight maids a-milking". day(ninth) --> "Nine ladies dancing". day(tenth) --> "Ten lords a-leaping". day(eleventh) --> "Eleven pipers piping". day(twelth) --> "Twelve drummers drumming". lyrics --> { Days = findall(D, $day(D,_,[])) }, stanzas(Days, []). stanzas([], _) --> []. stanzas([Day|Days], Prevs) --> "On the ", [Day.to_string], " day of Christmas\n", % convert atom day to string "My true love gave to me:\n", day(Day), previous_days(Prevs), "\n\n", stanzas(Days, [Day|Prevs]). previous_days([]) --> []. previous_days([D|Ds]) --> previous_days_(Ds, D). previous_days_([], D) --> " and\n", day(D). previous_days_([D|Ds], Prev) --> "\n", day(Prev), previous_days_(Ds, D).