lp.pro
1    %------------------------------------------------------------------------------
2    % File: lp.pro
3    % Type: Prolog source
4    % Line: List processing library
5    %------------------------------------------------------------------------------
6    
7    
8    % Writing elements of a list in order and in reverse order
9    %------------------------------------------------------------------------------
10   
11   % write_list(List) :: Write the elements of the list in order, one
12   % per line
13   
14   write_list([]).
15   write_list([H|T]) :-
16   	write(H),nl,
17     	write_list(T).
18   
19   % write_list_reversed(List) :: Write the elements of the list in
20   % reverse order, one per line
21   
22   write_list_reversed([]).
23   write_list_reversed([H|T]) :-
24     	write_list_reversed(T),
25     	write(H),nl.
26   
27   % Four explicit referencers
28   %------------------------------------------------------------------------------
29   
30   % first(List,First) :: Presuming the list is nonempty reference its
31   % first element
32   
33   first( [ First | _ ], First).
34   
35   % rest(List,Rest) :: Presuming the list is nonempty, reference the
36   % list except for the first element
37   
38   rest( [ _ | Rest ], Rest).
39   
40   % last(List,Last) :: Presuming the list is nonempty, reference the
41   % last element of the list
42   
43   last( [ S ], S).
44   last( [ _ | Rest ], Last) :- last(Rest, Last).
45   
46   % nth(N,List,NthElement) :: Presuming the list is nonempty, reference the
47   % nth element of the list
48   
49   nth(0, [ H | _ ], H).
50   nth(N, [ _ | T ], NthE) :- NM1 is N-1, nth(NM1,T,NthE).
51   
52   % item
53   %------------------------------------------------------------------------------
54   
55   % item(N,List,Item) :: Gets the Item of the List at the index N.
56   
57   item(N,[H|_],H) :- N = 0.
58   item(N,[_|T],E) :- N > 0, K is N - 1, item(K,T,E).
59   
60   % member
61   %------------------------------------------------------------------------------
62   
63   % member(Element,List) :: Displays all the Elements that are a member of the List.
64   member(X,[X|_]).
65   member(X,[_|Y]) :- member(X,Y).
66   
67   % count
68   %------------------------------------------------------------------------------
69   
70   % count(Element,List,Count) :: Count is the count of how many of Element there are in List.
71   
72   count(_,[],0).
73   count(Element,[Element|Rest],Count) :-
74     	count(Element,Rest,RestCount),
75     	Count is RestCount + 1.
76   count(Element,[_|Rest],Count) :-
77     	count(Element,Rest,Count).
78   
79   % append
80   %------------------------------------------------------------------------------
81   
82   % append(L1,L2,Result) :: Result has the items from list L2 appended to list L1. 
83   
84   append([],L,L).
85   append([H|T1],L2,[H|T3]) :- append(T1,L2,T3).
86   
87   % append(L1,L2,L3,Result) :: Result has the items from list L2 and L3 appended to list L1.
88   
89   append(L1,L2,L3,Result) :-
90     	append(L1,L2,L12),append(L12,L3,Result).
91   
92   % append(L1,L2,L3,L4,Result) :: Result has the items from list L2, L3 and L4 appended to list L1.
93   
94   append(L1,L2,L3,L4,Result) :-
95     	append(L1,L2,L3,L123),append(L123,L4,Result).
96   
97   % remove
98   %------------------------------------------------------------------------------
99   
100  % remove(E,L,Result) :: Removes the element E from the list L.
101  
102  remove(_,[],[]).
103  remove(First,[First|Rest],Rest):- !, remove(First,Rest,Rest).
104  remove(Element,[First|Rest],[First|RestLessElement]) :- remove(Element,Rest,RestLessElement).
105  
106  % replace
107  %------------------------------------------------------------------------------
108  
109  % replace(Position,Value,InList,OutList) :: Replaces element at index Position with element Value
110  %from list InList.
111  
112  replace(0,Object,[_|T],[Object|T]).
113  replace(ListPosition,Object,[H|T1],[H|T2]) :-
114  	K is ListPosition - 1,
115  	replace(K,Object,T1,T2).
116  
117  % make_list
118  %------------------------------------------------------------------------------
119  
120  % make_list(Size,Element,List) :: Makes a List of Size with Element.
121  
122  make_list(0,_,[]).
123  make_list(Length,Element,[Element|Rest]) :-
124  	K is Length - 1,
125  	make_list(K,Element,Rest).
126  
127  % reverse
128  %------------------------------------------------------------------------------
129  
130  % reverse(L,R) :: This takes list L and reverses i to create list R.
131  
132  reverse([],[]).
133  reverse([H|T],R) :-
134  	reverse(T,Rev),add_last(H,Rev,R).
135  
136  % add_first
137  %------------------------------------------------------------------------------
138  
139  % add_first(E,L,R) :: Adds element E to the beginning of list L.
140  
141  add_first(X,L,[X|L]).
142  
143  % add_last
144  %------------------------------------------------------------------------------
145  
146  % add_last(E,L,R) :: Adds element E to the end of list L.
147  
148  add_last(X,[],[X]).
149  add_last(X,[H|T],[H|TX]) :- add_last(X,T,TX).
150  
151  % pick
152  %------------------------------------------------------------------------------
153  
154  % pick(List,Item) :: Picks a random Item from List.
155  
156  pick(L,Item) :-
157    	length(L,Length),
158    	random(0,Length,RN),
159    	item(RN,L,Item).
160  
161  % take
162  %------------------------------------------------------------------------------
163  
164  % take(InList,OutElement,OutList) :: Picks a random OutElement to take out from Inlist.
165  
166  take(List,Element,Rest) :-
167    	pick(List,Element),
168    	remove(Element,List,Rest).
169  
170  % iota
171  %------------------------------------------------------------------------------
172  
173  % iota(K,IotaK) :: If K is not 0, Creates a list IoTak of 1 to element K.  
174  
175  iota(0,[]).
176  iota(N,IotaN) :-
177    	K is N - 1,
178    	iota(K,IotaK),
179    	add_last(N,IotaK,IotaN).
180  
181  % sum
182  %------------------------------------------------------------------------------
183  
184  % sum(ListOfNumbers,Sum) :: Adds up all the numbers in ListOfNumbers.
185  
186  sum([],0).
187  sum([Head|Tail],Sum) :-
188    	sum(Tail,SumOfTail),
189    	Sum is Head + SumOfTail.
190  
191  % product
192  %------------------------------------------------------------------------------
193  
194  % product(ListOfNumbers,Product) :: Multiplies all the numbers in ListOfNumbers.
195  
196  product([],1).
197  product([Head|Tail],Product) :-
198    	product(Tail,ProductOfTail),
199    	Product is Head * ProductOfTail.
200  
201  % factorial
202  %------------------------------------------------------------------------------
203  
204  % factorial(N,F) :: Calculates the factorial of number N.
205  
206  factorial(N,Nfactorial) :-
207    	iota(N,IotaN),
208    	product(IotaN,Nfactorial).
209  
210  % min
211  %------------------------------------------------------------------------------
212  
213  % min(NumericList,MinimumNumber) :: find the smallest number in the list
214  
215  min([Min],Min).                 
216  min([H|T],H) :-                 
217  	min(T,M),    
218      	H =< M. 
219  
220  min([H|T],M) :-                            
221      	min(T,M),  
222      	H > M. 
223  
224  % max
225  %------------------------------------------------------------------------------
226  
227  % max(NumericList,MinimumNumber) :: find the largest number in the list
228  
229  max([Max],Max). 
230  max([H|T],H) :-
231      	max(T,M),
232      	H > M.
233  
234  max([H|T],M) :-
235      	max(T,M),
236      	H =< M.
237  
238  % sort_inc
239  %------------------------------------------------------------------------------
240  
241  % sort_inc(UnorderedNumericList,OrderedNumericList) :: order low to high
242  
243  sort_inc([],[]).
244  sort_inc([E],[E]).
245  sort_inc(List,[Min|ListOut]):-
246    	min(List,Min),
247    	remove(Min,List,NewList),
248    	sort_inc(NewList,ListOut).
249  
250  % sort_dec
251  %------------------------------------------------------------------------------
252  
253  % sort_dec(UnorderedNumericList,OrderedNumericList) :: order high to low
254  
255  
256  sort_dec([],[]).
257  sort_dec([E],[E]).
258  sort_dec(List,[Max|ListOut]):-
259    	max(List,Max),
260    	remove(Max,List,NewList),
261    	sort_dec(NewList,ListOut).
262  
263  % alist
264  % Example:
265  % ?- alist([a,b,c,d],[1,2,3,4],A_List).
266  % A_List = [pair(a,1),pair(b,2),pair(c,3),pair(d,4)]
267  % alist(FirstList,SecondList,AssociationList) :: create the association list
268  % - (alist) from the two lists of equal length, the pairs of which are
269  % - encapsulated into terms with the name ’pair’
270  
271  alist(List,List2,FC_list) :-
272    	make_set(List,Set),
273    	alister(Set,List,List2,FC_list).
274  
275  alister([],_,_,[]).
276  alister([_|T],List,List2,AssociationList) :-
277      	first(List,Item),
278      	remove(Item,List,NewList),
279      	first(List2,Item2),
280      	remove(Item2,List2,NewList2),
281      	alister(T,NewList,NewList2,AssociationList),
282      	AssociationList = [pair(Item,Item2) | AssociationList].
283  
284  % assoc
285  %------------------------------------------------------------------------------
286  
287  % Example:
288  % ?- assoc([pair(a,1),pair(b,2),pair(c,3),pair(d,4)],c,Value).
289  % Value = 3
290  
291  % assoc(A_List,Key,Value) :: find the Value in the second slot corresponding
292  % - to the Key in the first slot of some association list pair
293  
294  assoc([],_,_).
295  assoc(A_List,Key,Value) :- 
296  	member(pair(Key,Value),A_List).
297  
298  % freq_count
299  %------------------------------------------------------------------------------
300  
301  % Example:
302  % ?- freq_count([a,b,a,b,c,a,b,c,d,a,b,c,d,e],FCL).
303  % FCL = [pair(a,4),pair(b,4),pair(c,3),pair(d,2),pair(e,1)]
304  % freq_count(List,FCL) :: Each element in List is counted and the count is paired with the element.
305  
306  freq_count(List,FC_list) :-
307    	make_set(List,Set),
308    	freq_counter(Set,List,FC_list).
309  
310  freq_counter([],_,[]).
311  freq_counter([H|T],List,FC_list) :-
312    	count(H,List,H_count),
313    	freq_counter(T,List,FCT_list),
314    	FC_list = [pair(H,H_count) | FCT_list].
315  
316  % make_set
317  %------------------------------------------------------------------------------
318  
319  % make_set(List,Set) :: If there are duplicate elements, 
320  %it makes a list of every possible set combination of elements in List.
321  
322  make_set([],[]).
323  make_set([H|T],TS) :-
324    	member(H,T),
325    	make_set(T,TS).
326  make_set([H|T],[H|TS]) :-
327    	make_set(T,TS).
328  
329  % flatten
330  %------------------------------------------------------------------------------
331  
332  % flatten(HL,FL) :: Takes elements that are a list in HL and takes the elements
333  % in that list and makes them elements of list HL. 
334  
335  flatten([],[]).
336  flatten([[]|T],L) :-
337    	flatten(T,L).
338  flatten([H|T],L) :-
339    	atom(H),
340    	flatten(T,Tflattened),
341    	append([H],Tflattened,L).
342  flatten([H|T],L) :-
343    	flatten(H,FlatHead),
344    	flatten(T,FlatTail),
345    	append(FlatHead,FlatTail,L).
346