LP Manual

NAME

lp - List processing prolog solutions

SYNOPSIS

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:

?- first([1,2,3,4,5], First). First = 1.

rest(List,T)

Get the tail of a list.
Arguments:

?- rest([1,2,3,4,5], Rest). Rest = [2, 3, 4, 5].

last_element(List,Last)

Get the last element of a list.
Arguments:

?- 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:

?- size([1,2,3,4,5], Size). Size = 5.

count(Element,List,Count)

Get the count of a given element in a list.
Arguments:

?- size([1,2,3,4,5], Size). Size = 5.

element_of(Element,List)

Return true if element is in the list.
Arguments:

?- 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:

?- 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:

?- 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:

?- 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:

?- 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:

?- 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:

?- 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:

?- add_last(1,[1,2,3,4,5],NL). NL = [1, 2, 3, 4, 5, 1] .

esrever(List,Reverse)

Reverse a list.
Arguments:

?- esrever([1,2,3,4,5],R). R = [5, 4, 3, 2, 1] .

join_lists(List1,List2,Join)

Joins two lists.
Arguments:

?- 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:

?- 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:

?- 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:

?- product([2,2,2,2],P). P = 16.

factorial(N,Nfactorial)

Find the factorial of N.
Arguments:

?- factorial(3,F). F = 6 . ?- factorial(4,F). F = 24 .

make_set(List,Set)

Make a list into a set by removing duplicates.
Arguments:

?- 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:

?- 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:

?- remove(四,[一,五,十,四,七,四],Remove). Remove = [一, 五, 十, 七, 四] .

take(List,Element,Rest)

Remove a random element from a list.
Arguments:

?- 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:

?- 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:

?- 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:

?- 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:

?- min([4,3,4,1,5,8],Min). Min = 1 .

max(List,Max)

Return the maximum number in a list.
Arguments:

?- max([4,3,4,1,5,8],Max). Max = 8 .

sort_inc(List,Result)

Sort a list from least to greatest.
Arguments:

?- 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:

?- 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:

?- 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:

?- assoc([pair(いち, 一), pair(に, 二), pair(さん, 三)],に,C). C = 二 .