0

So I have to write different procedures that will help me solve the farmer-wolf-goat-cabbage-fertilizer puzzle. For those of you that don't know it, it involves a farmer having to cross from the North bank of a river to the South bank with all the other objects. A bank is rendered safe in 3 situations: the farmer is present OR, the wolf is not left with the goat OR, the goat is not left behind with the cabbage. For the purpose of the exercise, the variables will be [f,b,g,w,c].

The procedure (choose(Bank, Items)) I am stuck at involves finding a list of 1 or 2 elements (always including the farmer - f), that could be part of a transport from a Bank without leaving it unsafe.

If one does choose([g,f,b], Items), the possible returned values for Items can be [f], [f,g], [f,b]. However, if we do choose([g,f,c], Items), the only possible values returned are [f,c] or [f,g], since the goat and cabbage cannot be left behind together.

Thus, could anyone please give me a hint how to get all possible options for Items but in lists no longer than 2 items?

false
  • 10,264
  • 13
  • 101
  • 209
Sorin Cioban
  • 2,237
  • 6
  • 30
  • 36
  • can you explicit the variables name please ? It's unclear presently ! – m09 Dec 01 '11 at 16:52
  • The variable names are just the initials of the objects - Farmer, Wolf, Goat, Cabbage, b for Bag of Fertilizer. Bank is just a list containing different objects. – Sorin Cioban Dec 01 '11 at 16:55

1 Answers1

1

I can't test right now but I guess that you could write something like :

choose(Bank, [f, Other]) :-
    select(f, Bank, Rest),
    select(Other, Rest, LeftBehind),
    safe(LeftBehind).
choose(Bank, [f]) :-
    select(f, Bank, LeftBehind),
    safe(LeftBehind).
m09
  • 7,490
  • 3
  • 31
  • 58
  • looks good, just don't forget you always have to take "f" with you. Also, can you make it so as to use append/3, please? Thanks :) – Sorin Cioban Dec 01 '11 at 17:55
  • I don't understand one thing though - Items is initially empty, why are we checking if it's a subset of Bank? Also, there always needs to be just one element taken from Bank, the other one in Items being f. f is always in Bank as otherwise a transport cannot be made. – Sorin Cioban Dec 01 '11 at 18:22
  • Is there any way I could contact you to ask you another question or two? Thanks :) – Sorin Cioban Dec 02 '11 at 17:13
  • you can contact me at this adress : q9zctmr8hxavarq@jetable.org it's available 1 day, don't want to let my real one in clear : ] – m09 Dec 02 '11 at 17:18
  • Thanks :) I emailed you from a gmail address. – Sorin Cioban Dec 02 '11 at 17:28
  • @SorinCioban : can you please resend the mail, it ended up in spam and I deleted it before I realized it was yours. – m09 Dec 02 '11 at 18:25