Created
December 5, 2020 03:21
-
-
Save katsaii/8aba9ad79f0ad24c2ffdbbe270204b51 to your computer and use it in GitHub Desktop.
My first prolog program!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
distance(A, B, Dist) :- | |
A = (X1, Y1), | |
B = (X2, Y2), | |
Dx is X2 - X1, | |
Dy is Y2 - Y1, | |
Dist is sqrt(Dx * Dx + Dy * Dy). | |
closest([X | Xs], Target, Min) :- | |
distance(X, Target, Dist), | |
closest_ext(Xs, Target, (X, Dist), Min). | |
closest_ext([], _, (Min, _), Min). | |
closest_ext([Candidate | Xs], Target, Current, Min) :- | |
Current = (_, CurrentDist), | |
distance(Candidate, Target, CandidateDist), | |
(CurrentDist < CandidateDist -> Next = Current ; Next = (Candidate, CandidateDist)), | |
closest_ext(Xs, Target, Next, Min). | |
main :- | |
Points = [(0, 0), (1, 1)], | |
closest(Points, (0, 1), Min), | |
write(Min), nl, | |
halt. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: | |
@swipl -o main.out -g main -c main.pl 2> /dev/null | |
run: all | |
@./main.out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment