{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

AccDiff_p2

# AccDiff_p2 - Parts Problem Assume we have a database of...

This preview shows pages 1–4. Sign up to view the full content.

Parts Problem Assume we have a database of assemblies required for a bike, for example: assembly(bike, [wheel, wheel, frame]). assembly(frame, [rearframe, frontframe]). assembly(frontframe, [fork, handle]). .... basicpart(rearframe). basicpart(fork). .... York University- CSE 3401- V. Movahedi 12 bike wheel hub gears axle bolt nut spoke rim frame frontframe fork handle rearframe 06_AccDiff

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Parts Problem (cont.) To find the parts to assemble a bike, we can write: partsof(X, [X]):- basicpart(X). partsof(X, P):- assembly(X, Subparts), partsofList(Subparts,P). partsofList([], []). partsofList([Head|Tail], P) :- partsof(Head, Headparts), partsofList(Tail, Tailparts), append(Headparts, Tailparts, P). Expensive computation York University- CSE 3401- V. Movahedi 13 06_AccDiff
Parts Problem (cont.) We can use an accumulator to avoid extra work: 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([H|Tail], A, P):- partsacc(H, A, Headparts), partsacclist(Tail, Headparts, P).

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}