It is important for any programming tool to have some functionality of handling collections like array, list or something else. Prolog use lists for the very purpose and I must warn C/C++ programmers (or even Java, C# or any other C-like language fan) that lists are not arrays but similar to it.
A list must be declared as list type in
domains section or in
predicate section, it should be mentioned that we are using list.
domains section, we do it by declaring a new domain and equaling it to a list type, declared by name of type followed by an asterisk; e.g.:
if one like to avoid domain section, but use list it should be mentioned in predicate section like this:
A list is presented as a comma-separated bracket closed tuple of same type:
List is not random access container but can be accessed in sequence. First element of a list can be accessed in kind of weird way:
If we pass a list there,
Head will contain the first element while
Tail contains another list excluding the first elemetn. Hence, we need to write some extra lines of code to access
I would like to avoid further explanation, which you can find a lot in the internet, but will go forward to placing an example code:
/*Example code on LIST in Prolog*/
H=Key,write(“Found at “,I),nl.
Unlike some of my fellow bloggers, I hold pure authorship to the codes placed here and these codes have never been cut pasted from some other site or plagiarized from any fellow student. Being frightened by some web publisher, I hereby have to claim my copyright on elements presented here.
All elements published here are published under NS7 Open Content License, which express that,
- if anyone use the content, partial or full in any context, s/he shall must mention the source and refer to the hyperlink
- any code under this license must not be published in any media unless modified significantly and that modification executes well enough without harm and credit to actual author is mentioned
- if anyone need to use code, published under this license, as example s/he can do it by mentioning actual author and shall not host a source file but refer to the original one, with another hyperlink to content accompanying, if accompanies one, the very code. As this code shall be used as example, content text should be relevant and example code should not be more than 40% of total content.
- Mentions to authors of codes and contents under this license must be in a good manner. Mention should be in same font of normal text of the content used in, font size should be of same size or larger than normal text of the content but in any case minimum size in electronic media is 12px or 11pt in print media. Normal text is regarded as that kind of text that comprises of a minimum of 70% of total content.
- contents presented here can be used for personal and academic purpose without any mention but shall not be published without taking proper permission from actual author
3 thoughts on “List in Prolog”
i want a code of searching for the value inside the list using its index
I think you are “smart” enough to find that it is already there in the given code.
What do I write in domain if I need to insert a list as list of list of integers, e.g., I want to have a variable which can take the value [1,2,[3,4],, [[]], 6]. Compiler Turbo Prolog. Thank You.