Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8489249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:46:37+00:00 2026-06-10T21:46:37+00:00

I’m reading the 7 Languages in 7 Days-book, and have reached the Prolog chapter.

  • 0

I’m reading the “7 Languages in 7 Days”-book, and have reached the Prolog chapter. As a learning exercises I’m trying to solve some textual logic puzzles. The puzzle goes as follow:

Five sisters all have their birthday in a different month and each on a different day of the week. Using the clues below, determine the month and day of the week each sister’s birthday falls.

  1. Paula was born in March but not on Saturday. Abigail’s birthday was not on Friday or Wednesday.
  2. The girl whose birthday is on Monday was born earlier in the year than Brenda and Mary.
  3. Tara wasn’t born in February and her birthday was on the weekend.
  4. Mary was not born in December nor was her birthday on a weekday. The girl whose birthday was in June was born on Sunday.
  5. Tara was born before Brenda, whose birthday wasn’t on Friday. Mary wasn’t born in July.

My current implementation probably looks like a joke to experienced Prolog programmers. The code is pasted below.

I would love some input on how to solve the question, and how to make the code both clear and dense.

Ie:

  1. How can I avoid typing out the limitations saying that the Days must be unique.
  2. How can I avoid typing out the limitations saying that the Months must be unique.
  3. Add the limitation about the ordering of the birthdays.
is_day(Day) :-
    member(Day, [sunday, monday, wednesday, friday, saturday]).

is_month(Month) :-
    member(Month, [february, march, june, july, december]).

solve(S) :-

    S = [[Name1, Month1, Day1],
         [Name2, Month2, Day2],
         [Name3, Month3, Day3],
         [Name4, Month4, Day4],
         [Name5, Month5, Day5]],

    % Five girls; Abigail, Brenda, Mary, Paula, Tara    
    Name1 = abigail,
    Name2 = brenda,
    Name3 = mary,
    Name4 = paula,
    Name5 = tara,

    is_day(Day1), is_day(Day2), is_day(Day3), is_day(Day4), is_day(Day5),
    Day1 \== Day2, Day1 \== Day3, Day1 \== Day4, Day1 \== Day5,
    Day2 \== Day1, Day2 \== Day3, Day2 \== Day4, Day2 \== Day5,
    Day3 \== Day1, Day3 \== Day2, Day3 \== Day4, Day3 \== Day5,
    Day4 \== Day1, Day4 \== Day2, Day4 \== Day3, Day4 \== Day5,

    is_month(Month1), is_month(Month2), is_month(Month3), is_month(Month4), is_month(Month5),
    Month1 \== Month2, Month1 \== Month3, Month1 \== Month4, Month1 \== Month5,
    Month2 \== Month1, Month2 \== Month3, Month2 \== Month4, Month2 \== Month5,
    Month3 \== Month1, Month3 \== Month2, Month3 \== Month4, Month3 \== Month5,
    Month4 \== Month1, Month4 \== Month2, Month4 \== Month3, Month4 \== Month5,

    % Paula was born in March but not on Saturday.  
    member([paula, march, _], S),
    Day4 \== sunday,

    % Abigail's birthday was not on Friday or Wednesday.    
    Day1 \== friday,
    Day1 \== wednesday,

    % The girl whose birthday is on Monday was born
    % earlier in the year than Brenda and Mary.

    % Tara wasn't born in February, and 
    % her birthday was on the weekend.
    Month5 \== february,
    Day5 \== monday, Day5 \== wednesday, Day5 \== friday,   

    % Mary was not born in December nor was her
    % birthday on a weekday.
    Month3 \== december,
    Day3 \== monday, Day3 \== wednesday, Day3 \== friday,

    % The girl whose birthday was in June was 
    % born on Sunday.
    member([_, june, sunday], S),

    % Tara was born before Brenda, whose birthday
    % wasn't on Friday.
    Day2 \== friday,

    % Mary wasn't born in July.
    Month3 \== july.

Update Based on the answer from chac I was able to solve the puzzle. Following the same recipe we (the programming language competency group at work) was able to solve a second puzzle as well. I have posted the complemete implementation, and example output as a gist on GitHub.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:46:38+00:00Added an answer on June 10, 2026 at 9:46 pm

    Maybe the riddle is underspecified, or your solution not complete: testing your code, I get

    ?- solve(X),maplist(writeln,X).
    [abigail,february,monday]
    [brenda,july,wednesday]
    [mary,june,sunday]
    [paula,march,friday]
    [tara,december,saturday]
    X = [[abigail, february, monday], [brenda, july, wednesday], [mary, june, sunday], [paula, march, friday], [tara, december, saturday]] ;
    [abigail,february,monday]
    [brenda,december,wednesday]
    [mary,june,sunday]
    [paula,march,friday]
    [tara,july,saturday]
    X = [[abigail, february, monday], [brenda, december, wednesday], [mary, june, sunday], [paula, march, friday], [tara, july, saturday]] 
    

    and yet more solutions. So when is brenda born?

    A ‘trick of the trade’ for uniqueness is using select/3 predicate, or simply permutation/2. Using this last the code becomes something like

    solve(S) :-
    
        S = [[Name1, Month1, Day1],
             [Name2, Month2, Day2],
             [Name3, Month3, Day3],
             [Name4, Month4, Day4],
             [Name5, Month5, Day5]],
    
        Girls =  [abigail, brenda, mary, paula, tara],
        Girls =  [Name1, Name2, Name3, Name4, Name5],
    
        Months = [february, march, june, july, december],
        Days =   [sunday, monday, wednesday, friday, saturday],
        permutation(Months, [Month1, Month2, Month3, Month4, Month5]),
        permutation(Days,   [Day1, Day2, Day3, Day4, Day5]),
    
        % Paula was born in March but not on Saturday.
        member([paula, march, C1], S), C1 \= saturday,
       ...
    

    the relation about ‘before in year’ can be coded like this:

        ...
        % The girl whose birthday is on Monday was born
        % earlier in the year than Brenda and Mary.
        member([_, C3, monday], S),
        member([brenda, C4, C10], S), before_in_year(C3, C4, Months),
        member([mary, C5, _], S), before_in_year(C3, C5, Months),
        ...
    

    with the service predicate

    before_in_year(X, Y, Months) :-
        nth1(Xi, Months, X),
        nth1(Yi, Months, Y),
        Xi < Yi.
    

    The ‘born in weekend’ can be coded like

    ...
    % Tara wasn't born in February, and
    % her birthday was on the weekend.
    member([tara, C6, C7], S), C6 \= february, (C7 = saturday ; C7 = sunday),
    
    % Mary was not born in December nor was her
    % birthday on a weekday.
    member([mary, C8, C9], S), C8 \= december, (C9 = saturday ; C9 = sunday),
    ...
    

    and so on. After this rewrite I get the unique solution

    ?- solve(X),maplist(writeln,X).
    [abigail,february,monday]
    [brenda,december,wednesday]
    [mary,june,sunday]
    [paula,march,friday]
    [tara,july,saturday]
    X = [[abigail, february, monday], [brenda, december, wednesday], [mary, june, sunday], [paula, march, friday], [tara, july, saturday]] ;
    false.
    

    edit

    I’ve noted just now that I introduced some redundant member/2 and free variables, like member([brenda, C4, C10], S),.... Those C4, C10 obiouvsly can be replaced by the variables bound to Brenda as Month2, Day2, as was in original code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.