Prolog: Finding the Nth Element in a List?

Louis, I'm not entirely clear on what you're aiming to do with this code, but a couple of comments that might hopefully help Things that start with a capital letter in Prolog are variables to be matched against in rules is a special symbol that can be used in place of a variable name to indicate that any value can match next_safe(_) is therefore only capable of providing you with a true/false answer if you give it a specific value. One of the major benefits of Prolog is the ability to unify against a variable through backtracking (as ony said). This would mean that when written correctly you could just ask Prolog next_safe(X) and it would return all the possible values (safe moves) that unify with X To go back to the first point about capital letters.

This means that OK is actually a variable waiting to be matched. It is effectively an empty box that you are trying to match against another empty box. I think what you're intending is to use the value ok which is different.

You do not assign to variables in the same way that you do in other programming styles. Something like the following might be closer to what you are looking for, though I'm still not sure it's right as it looks like you're trying to assign things but I'm not certain how your nth1 works Cells = ok, _, _, _, _, _ . ... next_safe(NewLoc) :- facing(CurrentDirection), delta(CurrentDirection, Delta), in_cell(OldLoc), NewLoc is OldLoc + Delta, nth1(NewLoc, Cells, ok).

Louis, I'm not entirely clear on what you're aiming to do with this code, but a couple of comments that might hopefully help. Things that start with a capital letter in Prolog are variables to be matched against in rules. _ is a special symbol that can be used in place of a variable name to indicate that any value can match.

Next_safe(_) is therefore only capable of providing you with a true/false answer if you give it a specific value. One of the major benefits of Prolog is the ability to unify against a variable through backtracking (as ony said). This would mean that when written correctly you could just ask Prolog next_safe(X).

And it would return all the possible values (safe moves) that unify with X. To go back to the first point about capital letters. This means that OK is actually a variable waiting to be matched.It is effectively an empty box that you are trying to match against another empty box.

I think what you're intending is to use the value ok which is different. You do not assign to variables in the same way that you do in other programming styles. Something like the following might be closer to what you are looking for, though I'm still not sure it's right as it looks like you're trying to assign things but I'm not certain how your nth1 works.

Cells = ok, _, _, _, _, _ . ... next_safe(NewLoc) :- facing(CurrentDirection), delta(CurrentDirection, Delta), in_cell(OldLoc), NewLoc is OldLoc + Delta, nth1(NewLoc, Cells, ok).

Nth0(1,1,2,3,X). X = 2.6? - listing(nth0).

Lists:nth0(A, B, C) :- integer(A),! , A>=0, nth0_det(A, B, C). Lists:nth0(A, B, C) :- var(A),!

, nth_gen(B, C, 0, A).True. Index listing start from 0 hope this helps ..

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions