Code Comments
Programming Forum and web based access to our favorite programming groups.Hi, First post on this board, apologies if I haven't adhered to any etiquett e rules, and hope that somebody can help me! I've got a problem that I just don't understand. I'm used to other programmi ng languages, but not Prolog. I am writing a program to navigate a path between several nodes in any order . my problem is that I have a list containing what it should contain, and then I use another function (which doesn't use this list at all), and then strai ght after this, the list contains something else! headTowards is a recursive function, which moves from one node to another (a journey). This works fine when run on its own. singleJourney is also recursive. Each iteration should move from the curren t position to a node from TargetList, and then remove that node from the lis t (storing the new list in NewTargetList and using that as the new TargetLis t when singleJourney is called again). Code listing and output below. Thanks in advance, BenExample output from the debug writes: List at A: [] List at B: [ (5, 3)] This then repeats a lot, because at point B, the list isn't empty, so the en d case is never called.code:
singleJourney(BoardSize, [], StartPos, Moves, Moves, N, NTot):- write('GOT HERE!'), nl, NTot is N. singleJourney(BoardSize, TargetList, StartPos, Moves, NewMoves, N, NTot):- member(Next, TargetList), del(Next, TargetList, NewTargetList), write('List at A: '), write(NewTargetList), nl, headTowards(BoardSize, StartPos, Next, Moves, B, 0, NMoves), write('List at B: '), write(NewTargetList), nl, N1 is N + NMoves, N1 < 6, singleJourney(BoardSize, NewTargetList, Next, B, NewMoves, N1, NTot). headTowards(BoardSize, Current, Current, Moves, NewMoves, N, N1):- N1 is 0. headTowards(BoardSize, Current, Target, Moves, NewMoves, N, NTot):- singleMove(BoardSize, Current, Next), not(member(Next, Moves)), N1 is N+1, N1 < 7, Moves1 = [Next | Moves], headTowards(BoardSize, Next, Target, Moves1, NewMoves, N1, NTot).
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.