Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Variables changing for no apparent reason!
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,
Ben

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).
Example 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.

Report this thread to moderator Post Follow-up to this message
Old Post
BenS
04-17-08 02:30 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Prolog archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Forum Jump:
All times are GMT. The time now is 05:06 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.