This is what I have.
distance([],[],0).
distance([V1|T1],[V2|T2], sqrt((V1-V2)^2 + D2)):-
distance([T1],[T2],D2).
It gives the out of global stack error. I also tried:
distance([V],[V],0).
distance([V1|T1],[V2|T2], D):-
D2 is distance([T1],[T2],D2),
D is sqrt((V1-V2)^2 + D2).
and it says that arguments are not sufficiently instantiated.
I'm trying to calculate sqrt((x1-y1)^2 + ... + (xn-yn)^2).
edit:
This is what I have now, it hangs up now.
distance([],[],0).
distance([X1|X2],[Y1|Y2],D):-
distance([X2],[Y2],D2),
D is sqrt((X1-Y1)^2 + D2).
edit:
I've got working now, thanks. This is my solution.
sumd([],[],0).
sumd([X|X2],[Y|Y2],D):- sumd(X2,Y2,D2), D is (X - Y)^2 + D2.
distance([X|X2],[Y|Y2],D):- sumd([X|X2],[Y|Y2],S), D is sqrt(S).
[X1|X2]represents a head element,X1, and a tail list,X2. So your recursive distance query should bedistance(X2, Y2, D2). That will fix your hang-up since, as it stands now, the first to arguments will never "drive" to[]since you always make a list of one element out of them. Your second issue will be your computation, you're going to havedistance = sqrt((x1-y1)^2 + sqrt((x2-y2)^2 + sqrt(...)))when it's done (nested square roots). - lurker