このディレクトリの索引 # I'm completely new to prolog and working on a homework assignment. # My program is supposed to take two lists of equal length, # and perform D = sqrt((X1-Y1)^2 + (X2-Y2)^2 + ... + (XN-YN)^2). # I've written the code to get the correct answer, but it's not presenting it correctly. # I think it may be a problem with the flow of the logic, since it seems like it's ending in an infinite loop. It's supposed to look like: # # ?- distance([1,2,3], [2,3,4], D). # D = 1.732051. # My code is giving me the correct result, but it prints it like: # # ?- distance([1,2,3],[2,3,4],D). # 1.732051 # true # And only because I have a line in there to print the result. # It also does not end (no period) until I hit enter, which is why I fear I have a loop. # How do I rephrase my code or redirect my logic, to get it to print normally? # # distance([],[],D) :- # F is sqrt(D), # format("~f~n", [F]). # # distance([A|T1], [B|T2], D) :- # var(D), # S is (A-B)*(A-B), # distance(T1, T2, S); # C is A-B, # E is C*C, # F is D+E, # distance(T1, T2, F). # distance(L1,L2) :- distance(L1,L2,0,D), F is sqrt(D), format("~f~n", [F]). distance([],[],D,D). distance([N1|R1],[N2|R2],D1,D) :- D2 is (N1-N2) ^ 2 + D1, distance(R1,R2,D2,D).