Domanda

I'm need to use SWI-prolog to solve a logic puzzle for homework, but I find the syntax and meaning very cumbersome even with my programming background. The problem I'm facing are error about singleton variables and the puzzle return false.

This is what i've done so far:

:- use_module(library(clpfd)).

sends(K,priscilla,C),
    C#\=rose.
sends(carol,L,rose).
sends(dick,L,sun).

sends(K,L,landscape).
sends(bob,rhonda,C).
sends(edna,quincy,C).

sends(K,simon,deer).
sends(K,tina,C).

solve :-
    sends(
        [alice,bob,carol,dick,edna],
        [priscilla,rhonda,quincy,simon,tina],
        [rose,heart,sun,landscape,deer]).

This is the question, a gentle nudge in the right direction or a answer which helps me understand the language better would be greatly appreciated! the question

È stato utile?

Soluzione

You're going to get a singleton error everywhere you have a single use variable. This appears to be your only use of variables, so I expect you get it a lot. Your code above is equivalent to:

sends(_,priscilla,C),
    C#\=rose.
sends(carol,_,rose).
sends(dick,_,sun).

sends(_,_,landscape).
sends(bob,rhonda,_).
sends(edna,quincy,_).

sends(_,simon,deer).
sends(_,tina,_).

If it looks weird or wrong with singleton variables replaced with _, it's a good indicator that you're missing logic. Do note that the arguments to sends/3 as you've written seem to be solely atomic, which makes this invocation especially queer:

sends(
    [alice,bob,carol,dick,edna],
    [priscilla,rhonda,quincy,simon,tina],
    [rose,heart,sun,landscape,deer]).

There's no reason to expect Prolog to magically convert this use of sends/3 with lists into some other invocation with atoms. I have no idea what you're expecting to happen here.

These kinds of puzzles are very popular assignment problems for Prolog. Search the archives here--there have been several just in the last few days--and you should find some inspiration.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top