Bike Configuration with SICStus-Prolog

:-use_module(library(lists)).

basic_part(brakes).
basic_part(frame).
basic_part(tire).
basic_part(nut).
basic_part(rim).
basic_part(spoke).

c(bike,[[wheel,2],[frame,1],[steering,1]]).
c(wheel,[[spoke,4],[rim,1],[tire,1],[nut,5]]).
c(steering,[[brakes,2],[nut,10]]).  

parts_required(N,P,[[P,N]]):-
                 basic_part(P).
parts_required(N,P,L):-
                      c(P,C),
                      parts_for_list(C,L1),
                      times(N,L1,L).
            
parts_for_list([],[]).
parts_for_list([[P,K]|T],L):-
                             parts_required(K,P,Lp),
                             parts_for_list(T,Lt),
                             combine(Lp,Lt,L).
                            
combine([],L,L).
combine([[P,N1]|T],L1,L):-
                          append(R1,[[P,N2]|R2],L1),
                          N is N1+N2,
                          append(R1,[[P,N]|R2],L2),
                          combine(T,L2,L),!. %??can we use combine(L2,T,L)here?
           
combine([[P,N]|T],L1,[[P,N]|L]):-
                                    combine(T,L1,L).
                                
times(_,[],[]).    
times(N,[[P,K]|T],[[P,M]|TN]):-
                               M is K*N,
                               times(N,T,TN).
                   

Comments »

The URI to TrackBack this entry is: http://liconey.blogsome.com/2007/12/03/bike-configuration-with-sicstus-prolog/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.