/* Twelve Days of Christmas 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 model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import v3_utils. main => go. % Prolog approach % (by kunstkritik) go ?=> lyrics, nl. go => true. % Picat approach go2 ?=> foreach(Day in 1..12) day(Day,DayStr,_What), printf("On the %w day of Christmas,%nMy true love gave to me:%n",DayStr), foreach(Prev in Day..-1..1) day(Prev,_,What), println(What) end, nl end, nl. go2 => true. % From https://www.reddit.com/r/prolog/comments/kip3wg/coding_challenge_27_2_weeks_the_twelve_days_of/ % (by kunstkritik) day(1, first, "A partridge in a pear tree"). day(2, second, "Two turtle doves and"). day(3, third, "Three french hens"). day(4, fourth, "Four calling birds"). day(5, fifth, "Five golden rings"). day(6, sixth, "Six geese a-laying"). day(7, seventh, "Seven swans a-swimming"). day(8, eighth, "Eight maids a-milking"). day(9, ninth, "Nine ladies dancing"). day(10,tenth, "Ten lords a-leaping"). day(11, eleventh, "Eleven pipers piping"). day(12, twelfth, "Twelve drummers drumming"). lyrics:- sing(1, []). stanza(N, Presents):- between(1,11,N), nl, sing(N, Presents). stanza(12, Presents):- sing(12, Presents). stanza(13, _). % The end sing(N0, PreviousPresents):- between(1,12,N0), day(N0, W, Present), printf("On the %w day of Christmas,%nMy true love gave to me:%n",W), maplist(println, [Present|PreviousPresents]), succ(N0, N1), % N1 = N0+1, stanza(N1, [Present|PreviousPresents]). succ(X,Y), var(X) ?=> X = Y-1. succ(X,Y), var(Y) ?=> Y = X+1.