NAME
lp - List processing prolog solutions
SYNOPSIS
- first(List,H)
- rest(List,T)
- last_element(List,Last)
- write_list(List)
- write_list_reversed(List)
- size(List,Size)
- count(Element,List,Count)
- element_of(Element,List)
- contains(List,Element)
- nth(N,List,Element)
- pick(List,Item)
- sum(List,Sum)
- make_list(Length,Element,List)
- iota(N,IotaN)
- add_first(X,L,List)
- add_last(X,L,List)
- esrever(List,Reverse)
- join_lists(List1,List2,Join)
- join_lists(List1,List2,List3,Join)
- join_lists(List1,List2,List3,List4,Join)
- product(List,Product)
- factorial(N,Nfactorial)
- make_set(List,Set)
- replace(ListPosition,Object,List,Result)
- remove(Element,List,Result)
- take(List,Element,Rest)
- split(PairList,FirstsList,SecondsList)
- min_pair(N1,N2,Min)
- max_pair(N1,N2,Max)
- min(List,Min)
- max(List,Max)
- sort_inc(List,Result)
- sort_dec(List,Result)
- a_list(FirstList,SecondList,AssociationList)
- assoc(AList,Key,Value)
LP is a collection of list processing programs. It is written in Prolog and therefore is intended to be used in an Prolog environment of some sort. It contains some alternative list processing programs to those already defined by the Prolog standards, along with a few more complex list processing programs.
DESCRIPTION
first(List,H)
Get the head of a list.
Arguments:
- List - non empty list to process
- H - Variable that will be bound to the head of the list
?- first([1,2,3,4,5], First).
First = 1.
rest(List,T)
Get the tail of a list.
Arguments:
- List - non empty list to process
- T - Variable that will be bound to the tail of the list
?- rest([1,2,3,4,5], Rest).
Rest = [2, 3, 4, 5].
last_element(List,Last)
Get the last element of a list.
Arguments:
- List - non empty list to process
- Last - Variable that will be bound to the tail of the list
?- last_element([1,2,3,4,5], Last).
Last = 5 .
write_list(List)
Write the elements of a list with each entry on a new line.
Arguments:
?- write_list([1,2,3,4,5]).
1
2
3
4
5
true.
write_list_reversed(List)
Write the elements of a list with each entry on a new line in reverse.
Arguments:
?- write_list_reversed([1,2,3,4,5]).
5
4
3
2
1
true.
size(List,Size)
Get the size of a list.
Arguments:
- List - list to process
- Size - Variable that will be bound to the size of the list
?- size([1,2,3,4,5], Size).
Size = 5.
count(Element,List,Count)
Get the count of a given element in a list.
Arguments:
- Element - element to get count of
- List - list to process
- Count - Variable that will be bound to the count of elements in list
?- size([1,2,3,4,5], Size).
Size = 5.
element_of(Element,List)
Return true if element is in the list.
Arguments:
- Element - element to search for
- List - list to search through
?- element_of(3,[1,2,3,4,5]).
true .
?- element_of(0,[1,2,3,4,5]).
false.
contains(List,Element)
Return true is a list contains the element.
Arguments:
- List - list to search through
- Element - element to search for
?- contains([1,2,3,4,5],3).
true .
?- contains([1,2,3,4,5],0).
false.
nth(N,List,Element)
Return the nth element of a list.
Arguments:
- N - zero-based index of the element to grab
- List - list to search through
- Element - Variable that will be bound to the nth element
?- nth(1,[1,2,3,4,5],N).
N = 2 .
?- nth(5,[1,2,3,4,5],N).
false.
pick(List,Item)
Pick a random item from a list.
Arguments:
- List - list to pick from
- Item - Variable that will be bound to a random item in the list
?- pick([1,2,3,4,5],P).
P = 1 .
?- pick([1,2,3,4,5],P).
P = 3 .
sum(List,Sum)
Add all the numbers in a list together.
Arguments:
- List - list to sum
- Sum - Variable that will be bound to the sum of all the elements in the list
?- sum([1,2,3,4,5],S).
S = 15.
make_list(Length,Element,List)
Return a list of a single element repeated a number of times.
Arguments:
- Length - number of times to repeat element
- Element - element to be repeated
- List - Variable that will be bound to the generated list
?- make_list(5,0,L).
L = [0, 0, 0, 0, 0] .
add_first(X,L,List)
Add an element to the beginning of a list.
Arguments:
- X - element to add
- L - list to add to
- List - Variable to be bound to the resulting list
?- add_first(1,[1,2,3,4,5],NL).
NL = [1, 1, 2, 3, 4, 5].
add_last(X,L,List)
Add an element to the end of a list.
Arguments:
- X - element to add
- L - list to add to
- List - Variable to be bound to the resulting list
?- add_last(1,[1,2,3,4,5],NL).
NL = [1, 2, 3, 4, 5, 1] .
esrever(List,Reverse)
Reverse a list.
Arguments:
- List - list to reverse
- Reverse - Variable to be bound to the reverse of the list
?- esrever([1,2,3,4,5],R).
R = [5, 4, 3, 2, 1] .
join_lists(List1,List2,Join)
Joins two lists.
Arguments:
- List1 - list at the front
- List2 - list at the back
- Join - Variable to be bound to the list after joining
?- join_lists([1,2,3],[4,5,6],J).
J = [1, 2, 3, 4, 5, 6].
join_lists(List1,List2,List3,Join)
Joins three lists.
Arguments:
- List1 - list at the front
- List2 - list in the middle
- List3 - list at the back
- Join - Variable to be bound to the list after joining
?- join_lists([1,2,3],[4,5,6],[7,8,9],J).
J = [1, 2, 3, 4, 5, 6, 7, 8, 9].
join_lists(List1,List2,List3,List4,Join)
Joins four lists.
Arguments:
- List1 - first list to be joined
- List2 - second list to be joined
- List3 - third list to be joined
- List4 - fourth list to be joined
- Join - Variable to be bound to the list after joining
?- join_lists([1,2],[3,4],[5,6],[7,8],J).
J = [1, 2, 3, 4, 5, 6, 7, 8].
product(List,Product)
Multiply all numbers in a list together.
Arguments:
- List - list to be multiplied
- Product - Variable to be bound to the result of the multiplication
?- product([2,2,2,2],P).
P = 16.
factorial(N,Nfactorial)
Find the factorial of N.
Arguments:
- N - factorial to find
- Nfactorial - Variable to be bound to the factorial of N
?- factorial(3,F).
F = 6 .
?- factorial(4,F).
F = 24 .
make_set(List,Set)
Make a list into a set by removing duplicates.
Arguments:
- List - list to make set from
- Set - Variable to be bound to the set made by the list
?- make_set([1,2,2,4],T).
T = [1, 2, 4] .
replace(ListPosition,Object,List,Result)
Replace an element at a given position in a list with another element.
Arguments:
- ListPosition - index of element to replace
- Object - element to put in place of the replaced item
- List - list to replace an item from
- Result - Variable to be bound to the list after replacement
?- replace(4,foo,[a,b,c,d,e,f],R).
R = [a, b, c, d, foo, f] .
remove(Element,List,Result)
Remove the first instance of a given element from a list.
Arguments:
- Element - element to remove from list
- List - list to remove element from
- Result - Variable to be bound to the list after removal
?- remove(四,[一,五,十,四,七,四],Remove).
Remove = [一, 五, 十, 七, 四] .
take(List,Element,Rest)
Remove a random element from a list.
Arguments:
- List - list to remove element from
- Element - Variable to be bound to the element to remove from list
- Rest - Variable to be bound to the list after removal
?- take([1,2,3,4,5],E,R).
E = 4,
R = [1, 2, 3, 5] .
?- take([1,2,3,4,5],E,R).
E = 3,
R = [1, 2, 4, 5] .
split(PairList,FirstsList,SecondsList)
Split a list of pairs into two lists where the first list contains the first element in each pair and the second list contains the second element in each pair.
Arguments:
- PairList - list of pairs to split
- FirstsList - Variable to be bound to the list containing the first position pair elements
- SecondsList - Variable to be bound to the list containing the second position pair elements
?- split([[one,一],[two,二],[three,三]],E,Ni本語).
E = [one, two, three],
Ni本語 = [一, 二, 三].
min_pair(N1,N2,Min)
Return the lesser of a pair of numbers.
Arguments:
- N1 - first number to compare
- N2 - second number to compare
- Min - Variable to be bound to the lessor of the pair
?- min_pair(2,4,Min).
Min = 2 .
?- min_pair(4,2,Min).
Min = 2.
max_pair(N1,N2,Max)
Return the greater of a pair of numbers.
Arguments:
- N1 - first number to compare
- N2 - second number to compare
- Max - Variable to be bound to the greater of the pair
?- max_pair(2,4,Max).
Max = 4.
?- max_pair(4,2,Max).
Max = 4 .
min(List,Min)
Return the minimum number in a list.
Arguments:
- List - list to search
- Min - Variable to be bound to the minimum of the list
?- min([4,3,4,1,5,8],Min).
Min = 1 .
max(List,Max)
Return the maximum number in a list.
Arguments:
- List - list to search
- Max - Variable to be bound to the maximum of the list
?- max([4,3,4,1,5,8],Max).
Max = 8 .
sort_inc(List,Result)
Sort a list from least to greatest.
Arguments:
- List - list to sort
- Result - Variable to be bound to the sorted list
?- sort_inc([8,2,14,5,3],Si).
Si = [2, 3, 5, 8, 14].
sort_dec(List,Result)
Sort a list from greatest to least.
Arguments:
- List - list to sort
- Result - Variable to be bound to the sorted list
?- sort_dec([8,2,14,5,3],Sd).
Sd = [14, 8, 5, 3, 2].
a_list(FirstList,SecondList,AssociationList)
Create an association list from two lists of equal lengths, the first considered to be the keys, the second to be the values.
Arguments:
- FirstList - list that will used for the keys
- SecondList - list that will used for the values
- AssociationList - Variable to be bound to the resulting association list
?- a_list([いち,に,さん],[一,二,三], Pairs).
Pairs = [pair(いち, 一), pair(に, 二), pair(さん, 三)] .
assoc(AList,Key,Value)
Find the value in the second slot corresponding to Key in the first slot of some pair of the association list.
Arguments:
- AList - association list to search through
- Key - key to search for
- Value - Variable to be bound to the value corresponding to the given key
?- assoc([pair(いち, 一), pair(に, 二), pair(さん, 三)],に,C).
C = 二 .