{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full Document Right Arrow Icon
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
Background image of page 2
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).
Background image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}