Last active
June 19, 2021 01:47
-
-
Save EmmanuelOga/bca216ea49f6d3f00b7929757f3b2d93 to your computer and use it in GitHub Desktop.
A bike in prolog
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
basicpart(rim). | |
basicpart(spoke). | |
basicpart(rearframe). | |
basicpart(handles). | |
basicpart(gears). | |
basicpart(bolt). | |
basicpart(nut). | |
basicpart(fork). | |
assembly(bike, [wheel, wheel, frame]). | |
assembly(wheel, [spoke, rim, hub]). | |
assembly(frame, [rearframe, frontframe]). | |
assembly(frontframe, [fork, handles]). | |
assembly(hub, [gears, axle]). | |
assembly(axle, [bolt, nut]). | |
/* | |
With no accumulators: | |
partsof(X, [X]) :- basicpart(X). | |
partsof(X, P) :- assembly(X, Subparts), partsoflist(Subparts, P). | |
partsoflist([], []). | |
partsoflist([P|Tail], Total) :- | |
partsof(P, Headparts), | |
partsoflist(Tail, Tailparts), | |
append(Headparts, Tailparts, Total). | |
*/ | |
/* | |
With accumulators: | |
partsof(X, P) :- partsacc(X, [], P). | |
partsacc(X, A, [X|A]) :- basicpart(X). | |
partsacc(X, A, P) :- assembly(X, Subparts), partsacclist(Subparts, A, P). | |
partsacclist([], A, A). | |
partsacclist([P|Tail], A, Total) :- | |
partsacc(P, A, Hp), | |
partsacclist(Tail, Hp, Total). | |
*/ | |
/* | |
With difference lists: | |
*/ | |
partsof(X, P) :- partsacc(X, P, Hole), Hole = []. | |
partsacc(X, [X|Hole], Hole) :- basicpart(X). | |
partsacc(X, P, Hole) :- | |
assembly(X, Subparts), | |
partsacclist(Subparts, P, Hole). | |
partsacclist([], Hole, Hole). | |
partsacclist([P|T], Total, Hole) :- | |
partsacc(P, Total, Hole1), | |
partsacclist(T, Hole1, Hole). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment