OUTPUT
[oulu, kajaani, joensuu] total length 413 km
STACK
⏩ road(joensuu, kajaani, 230). % 230 km road connection from Joensuu to Kajaani
⏩ road(kajaani, oulu, 183). % 183 km road connection from Kajaani to Oulu
⏩ member(H, [H|_]). % 1
⏩ member(X, [_|T]) :- % 2
⏩ member(X, T). →
⏩ member(X, [_|T]) :- % 2
⏩ member(X, T). →
⏩ connected(Town1, Town2, Length) :- % 2
⏩ road(Town2, Town1, Length). →
⏩ connected(oulu, TownX₂, Length1₂) :- % 1
⏩ road(oulu, TownX₂, Length1₂). →
⏩ route(Town₉, Town₉, _, [Town₉], 0). % 1
⏩ route(kajaani, joensuu, [oulu|[]], [kajaani|FromTown1₆], Length2₂) :- % 2
⏩ ToTownX₆ = [kajaani|[oulu]],
⏩ connected(kajaani, TownX₆, Length1₆), →
⏩ not(member(TownX₆, ToTownX₆)), →
⏩ route(TownX₆, joensuu, ToTownX₆, FromTown1₆, Length2₆), →
⏩ Length2₂ = Length1₆ + Length2₆.
⏩ route(Town1₂, Town2₂, ToTown1₂, [Town1₂|FromTown1₂], Len₂) :- % 2
⏩ ToTownX₂ = [Town1₂|ToTown1₂],
⏩ connected(Town1₂, TownX₂, Length1₂), →
⏩ not(member(TownX₂, ToTownX₂)), →
⏩ route(TownX₂, Town2₂, ToTownX₂, FromTown1₂, Length2₂), →
⏩ Len₂ = Length1₂ + Length2₂.
⏩ route(Town, Town, _, [Town], 0). % 1
⏩ :- route(oulu, joensuu, [], Route₁, Len₁), →
⏩ writeln(Route₁, ", total length ", Len₁, " km").
DATABASE
road(joensuu, kajaani, 230). % 230 km road connection from Joensuu to Kajaani
road(kajaani, oulu, 183). % 183 km road connection from Kajaani to Oulu
member(H, [H|_]). % 1
member(X, [_|T]) :- % 2
member(X, T).
connected(Town1, Town2, Length) :- % 1
road(Town1, Town2, Length).
connected(Town1, Town2, Length) :- % 2
road(Town2, Town1, Length).
route(Town, Town, _, [Town], 0). % 1
route(Town1, Town2, ToTown1, [Town1|FromTown1], Length) :- % 2
ToTownX = [Town1|ToTown1],
connected(Town1, TownX, Length1),
not(member(TownX, ToTownX)),
route(TownX, Town2, ToTownX, FromTown1, Length2),
Length = Length1 + Length2.
:- route(oulu, joensuu, [], Route, Len),
writeln(Route, ", total length ", Len, " km").
Ready. Push Step to start animating a Jalog (Prolog) program.
By: Mikko Levanto and Ari Okkonen 2019
Jalog home:
https://jalogteam.github.io/