Reputation: 1
So I'm given a maze and I am trying to find out the number of open neighbors (adjacent spots that have the character 'g' or '-').
openNeighbors(Maze,(R,C),Answer):-
openCount(Maze,(R-1,C),Up),
openCount(Maze,(R+1,C),Down),
openCount(Maze,(R,C-1),Left),
openCount(Maze,(R,C+1),Right),
Answer is Up+Down+Left+Right.
openCount(Maze,Space,Count):-
find2D(Maze,What,Space),
( What =:= - ->
Count is 1
; What =:= g ->
Count is 1
; Count is 0
)
My thought process is that I could just call the openCount function for up, down, left, and right but how do I format my if statement within the openCount function? I'm not sure how to compare my 'What' variable with 'g' and '-'.
The Java equivalent for what I want to do is:
if(What.equals('g')||What.equals('-')){
return 1;
}else{
return 0;
}
Upvotes: 0
Views: 191
Reputation: 2162
aaargh....
Do yourself a favor - right now, banish -> from your mind. It was introduced to make it easy to avoid one line predicates, and for experienced prolog programmers, it's ok (and can avoid grue choice points, fwiw).
=:= is 'numerically equal', as in 5 =:= 3+2
What =:= g
will freak out with 'g is not a function'. = is 'unify' - which tries to force the sides to be the same thing, and fails if it can't. == is 'same', which succeeds if it is sure the sides are the same. Like, do you and I live in the same kind of building? Quite possible, we both probably live in an apartment or a house, but we don't know. It never binds things.
You also might want to do arithmetic. 'is' is the operator for that.
X is 5+ 6*Y
notice that you can't do arithmetic automatically, like in lesser languages - calling foo(5 + Y) doesn't work, it passes '+'(5,Y) to foo. This inconvenience is countered by lots of wonderfulness, notably that foo can decide what to do with it's arguments. So matrix_math(5*X + Y*Z) could evaluate matrices. You can make up the language as you go. 8cD this is good.
Somewhere in the class notes is probably the fact that units of code encapsulation are called predicates, not 'functions'.
So, while I assume when you talk about functions, you're meaning predicates, but hopefully the maintainer who wrote 'g is not a function' knows the proper terminology and really meant function here.
Yup - that's because they meant the built in functions, of which there are a few. They go into arithmetic formula.
Height is Radius * sin(Theta)
sin is the sin function. You can't define your own functions. If you were on a prolog with no sin function and wanted to define it you'd need to return the value somehow and use a predicate.
sin(Theta, SinTheta) :- ... some code ... .
I'm sure at the moment all of this seem arbitrary and slightly crazy.
The parens around the second argument of openCount aren't useful. and you don't want to pass openCount '+'(C,1)
succ(Prev,Next) is a useful built-in predicate, 'successor of'
succ(Left, C), succ(C, Right), succ(Above, R), succ(R, Below)
I really encourage indenting the body of the rule one level.
Finally, predicate_names_are_like_this_by_convention. thisTooIsAnAtomSoItsAPredicateNameButDontDoIt. <-- you are doing this ThisIsRightOutAndWontCompile.
Upvotes: 1