Skip to content

Instantly share code, notes, and snippets.

@EmmanuelOga
Last active June 19, 2021 01:47
Show Gist options
  • Save EmmanuelOga/bca216ea49f6d3f00b7929757f3b2d93 to your computer and use it in GitHub Desktop.
Save EmmanuelOga/bca216ea49f6d3f00b7929757f3b2d93 to your computer and use it in GitHub Desktop.

Revisions

  1. EmmanuelOga revised this gist Jun 19, 2021. 1 changed file with 17 additions and 1 deletion.
    18 changes: 17 additions & 1 deletion bike.pl
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,8 @@
    append(Headparts, Tailparts, Total).
    */

    /* With accumulators: */
    /*
    With accumulators:
    partsof(X, P) :- partsacc(X, [], P).
    partsacc(X, A, [X|A]) :- basicpart(X).
    @@ -37,3 +38,18 @@
    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).
  2. EmmanuelOga revised this gist Jun 19, 2021. 1 changed file with 16 additions and 1 deletion.
    17 changes: 16 additions & 1 deletion bike.pl
    Original file line number Diff line number Diff line change
    @@ -14,11 +14,26 @@
    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).
    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).
  3. EmmanuelOga created this gist Jun 19, 2021.
    24 changes: 24 additions & 0 deletions bike.pl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    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]).

    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).