This file provides you with a way to do list processing in prolog. There are 25 functions that you can use to process lists, and they can be broken up into four categories: constructors, modifiers, informers, and numeric. It is important to note that if you pass variables into these functions, the operations are not modifying the variables, but creating a new list with the values bound to them, and binding them to another variable that is passed into each function.
You can view the code for these functions here
For any new to prolog, you load the file with:
consult(lp.pl).
Function executions look like:
function(Variables).
Lists look like:
[index0,index1,index2,index3]
These functions construct new lists either from one or more lists, a list and a new element, or an element and the size of the list.
makelist(Size,Element,List)
Creates a list that consists of the value passed to Element repeated Size times, binding it to List. A number must be passed into Size, and anything can be passed into Element, and List must be a variable name.
makelist(4,happy,List). will return:
List = [happy,happy,happy,happy]
append(List1,List2,Result)
Creates a list that is List2 added onto the end of List1, binding it to Result. Both List1 and List2 must be lists in order for the function to work. This function can take up to four lists to be appended, so it can also take the forms: append(List1,List2,List3,Result) append(List1,List2,List3,List4,Result)
append([1,2,3,4][5,6,7],List). will return:
List = [1,2,3,4,5,6,7]
alist(List1,List2,AList)
Creates an association list from List1 and List2 by pairing each of the elements from the lists with their respective element from the other list like so: pair(Element1,Element2). This list is then bound to whatever variable is passed into AList. List1 and List2 must be lists of equal length in order for this function to work. You may also pass an association list into AList with two variable names into List1 and List2 to see what the two lists that would make that association list are.
alist([ace,2,3,4],[spades,hearts,clubs,diamonds],AList). will return:
AList = [pair(ace,spades),pair(2,hearts),pair(3,clubs),pair(4,diamonds)]
lastput(Element,List,Result)
Creates a new list bound to Result that is List with Element added to the end. Element has no restrictions on what it must be, but List must be a list with at least one element, and Result must be a variable.
lasput(a,[b,c,d,e],List). will return:
List = [b,c,d,e,a]
These functions modify existing lists by removing elements, changing their order, or replacing them.
remove(Element,List,Result)
Binds the list List with the first occurance of Element removed to Result. List must be a list in order for this function to work,and the function will return false if Element is not an element of List.
remove(3,[1,2,3,4,3,5,6],NewList). will return:
NewList = [1,2,4,3,5,6]
make_set(List,Set)
Recursively removes ay duplicates in list List, and binds the resulting list to Set. List must be a list in order for this function to work, and Set must be a variable that the set can be bound to.
make_set([red,blue,yellow,orange,red,purple,blue,green,red],Set). will return:
Set = [yellow,orange,purple,blue,green,red]
replace(Index,Object,List,Result)
Replace the value at list position Index to Object, binding the new list to Result. In order for this function to succeed, Index must be a number, List must be a list, and Result must be a variable.
replace(2,pear,[banana,orange,apple,grape],NewList). will return:
NewList = [banana,orange,pear,grape]
reverse(List,Result)
Reverse the order of the elements of List and assign the new list to Result. While this function will succeed if you pass a list into Result, and a variable into List, it is recommended to pass a list into List and a variable into Result.
reverse([f,e,d,c,b,a],Reversed). will return:
Reversed = [a,b,c,d,e,f]
take(List,Element,Rest)
Remove a random element from a list, and bind it to Element. In order for this function to succeed, a list must be passed into List, and two different variables must be passed into Element and Rest. This is different from the other functions mentioned so far, as it has many possible outputs. As such, the example will show all possible outputs.
take([apple,orange,banana],Element,Rest). can return:
Element = apple,
Rest = [orange,banana];
Element = orange,
Rest = [apple,banana];
Element = banana,
Rest = [apple,orange].
flatten(List,FlatList)
Takes a 2-dimensional list and creates one list out of all of the elements of its elements, binding it to FlatList. This function takes in a list of lists(it can accept a normal list, but nothing will be done to it) into List, and FlatList must be a variable. It will not work if you pass a list into FlatList and a variable into List, so be sure to pass them in in the proper order.
flatten([[a,b,c,d],[e,f,g,h],[i,j,k,l]],Flattened). will return:
Flettened = [a,b,c,d,e,f,g,h,i,j,k,l].
These functions provide the user with information about the list, or some element of the list.
writelist(List)
This function will write out every element of a list, with a new line for each of them. You must pass a list into this function in order for it to work, otherwise it will return false, signifying failure.
writelist([cyan,blue,green,yellow,orange,magenta,red]). will return:
cyan
blue
green
yellow
orange
magenta
red
member(Element,List)
Returns true if the value passed to Element is an element of the list List. In order for this function to succeed as intended, you must pass a value into Element, and a list into List. Should you pass a variable into Element, you could use this function to list all of the elements of the List you pass in, like the writelist function above, but it is recommended you use writelist or some other function for that behavior.
member(1,[1,2,3]). will return:
true.
member(grapefruit,[banana,orange,apple,grape]). will return:
false.
size(List,Size)
Determines the length of a list and binds it to Size. In order for this function to succeed, you must pass a list into List, and a variable into Size. Prolog also has a built-in length(List) function, which has an identical signature, so you may use these two interchangeably if you have this file loaded.
size([one,2,three,4],Size). will return:
Size = 4.
item(Index,List,Result)
Gets the element at list position Index from List and binds it to Result. In order for this function to succeed, you must pass a number between 0 (inclusive) and the length of the list minus 1(inclusive) into Index (0 Index size(List)). List must also be a list that has at least one item, inserting the empty list will result in a failure, and Result must be a variable.
item(3,[one,1,2,two,three,3],Item). will return:
Item = 2.
last(List,Element)
Gets the last element of a list, and binds it to Element. In order for this function to succeed, you must pass in a list with at least one element into List, and a variable into Element.
last([apple,banana,orange],Last). will return:
Last = orange.
pick(List,Item)
Retrieves a random element from a list, and binds it to Item. In order for this function to succeed, you must pass a list into List, and a variable into Item. Much like the take function defined in the previous section (which uses this function), this function has as many possible outcomes as there are elements in the List you use.
take([apple,orange,banana],Element,Rest). can return:
Element = apple ;
Element = orange ;
Element = banana.
assoc(AList,Key,Result)
Retrieves the value of the second position of a pair in an association list, given the value that is in the first position, and binds it to Result. This function requires that you pass it an association list, of the form generated from alist, into AList, and the value that you wish to find the companion to into Key. If you enter a value that is not in the first position of one of the pairs in the list, the function will return false, signifying that there is no value that matches that key.
assoc([pair(ace,spades),pair(2,hearts),pair(3,clubs),pair(4,diamonds)],3,Value). will return:
Value = clubs.
All of these functions deal with lists of numbers and arithmetic, and as such cannot use non-numeric values.
iota(Number,List)
Creates a list of numbers starting at 1 and ending at Number, binding the list to List. Number must have a numeric value passed to it, and List must be passed a variable in order for this function to succeed.
iota(7,Iota). will return:
Iota = [1,2,3,4,5,6,7]
sum(List,Sum)
Adds all of the elements of a list and binds the resulting value to Sum. List must be passed a numeric list (or the empty list), and Sum must be passed a variable in order for this function to succeed.
sum([12,15,10],Sum). will return:
Sum = 37.
product(List,Product)
Multiplies all of the elements of a list and binds the resulting value to Product. List must be passed a numeric list with at least one element, and Product must be passed a variable in order for this function to succeed.
product([5,6,2],Product). will return
Product = 60.
min(List,Smallest)
Retrieves the smallest number from List and binds it to Smallest. In order for this function to succeed, List must be passed a numeric list with at least element, and Smallest must be passed a variable.
min([5,3,6,10,12,13,9],Smallest). will return:
Smallest = 3.
max(List,Largest)
Retrieves the largest number from List and binds it to Largest. In order for this function to succeed, List must be passed a numeric list with at least element, and Largest must be passed a variable.
max([5,3,6,10,12,13,9],Largest). will return:
Largest = 13.
sort_inc(List,Sorted)
Creates a new list with the elements of List sorted from lowest to highest and binds the list to Sorted. List must be a numeric list in order for this to succeed.
sort_inc([5,3,6,10,12,13,9],Sorted). will return:
Sorted = [3,5,6,9,10,12,13].
sort_dec(List,Sorted)
Creates a new list with the elements of List sorted from highest to lowest and binds the list to Sorted. List must be a numeric list in order for this to succed.
sort_dec([5,3,6,10,12,13,9],Sorted). will return:
Sorted = [13,12,10,9,6,5,3].