0

I'm trying to solve the fox, goose, and grain riddle (where the farmer can only transport himself and one of the three across the river), and am following the code given here: http://hilltop.bradley.edu/~chris/prolog.html (about 4/5 of the way into the document)

Basically the code tells Prolog which moves are valid and Prolog then calculates the answer from that.

%[Farmer_position, Fox_position, Goose_position, Grain_position]
graph([w,w,w,w],[e,w,e,w]).
graph([e,w,e,w],[w,w,e,w]).
graph([w,w,e,w],[e,w,e,w]).
graph([w,w,e,w],[e,e,e,w]).
graph([w,w,e,w],[e,w,e,e]).
graph([e,e,e,w],[w,w,e,w]).
graph([e,e,e,w],[w,e,w,w]).
graph([e,w,e,e],[w,w,w,e]).
graph([e,w,e,e],[w,w,e,w]).
graph([w,w,w,e],[e,e,w,e]).
graph([w,w,w,e],[e,w,e,e]).
graph([e,e,w,e],[w,e,w,e]).
graph([e,e,w,e],[w,w,w,e]).
graph([e,e,w,e],[w,e,w,w]).
dfs([w,w,w,w],[[e,e,e,e]],answer).
?- dfs([w,w,w,w],[[e,e,e,e]],answer).

However, I can't get Prolog to display the answer. Am I doing something wrong? I'm using SWI-Prolog and as an IDE I'm using SWI-Prolog-Editor.

EDIT: Just to be clear, it compiles, it's just not showing the "value" of the "answer" variable.

false
  • 10,264
  • 13
  • 101
  • 209
janvdl
  • 300
  • 1
  • 10
  • Variables start with an Uppercase ! Try `dfs([w,w,w,w],[[e,e,e,e]],Answer).` – CapelliC Oct 27 '13 at 16:44
  • Thanks, but in that case I get: Warning: Singleton variables: Answer – janvdl Oct 27 '13 at 16:59
  • I mean, when you query: `?- dfs([w,w,w,w],[[e,e,e,e]],Answer).` – CapelliC Oct 27 '13 at 17:00
  • Still the same singleton warning I'm afraid. – janvdl Oct 27 '13 at 17:01
  • You haven't assigned anything to `Answer`. It's just sitting there as a parameter to `dfs` and `dfs` has no logic which assigns it. – lurker Oct 27 '13 at 22:08
  • I assumed that dfs would assign the solution into answer. I'm sorry, I'm new to prolog and am trying to adapt the Fox/Goose/Grain problem to solve a different problem. Could you perhaps tell me how to work around this problem? – janvdl Oct 28 '13 at 15:27

0 Answers0