Category: Programming Language Prolog

  • Decomposing Atoms

    Decomposing Atoms

    Atom decomposing is just the reverse of constructing atoms. It helps us to get a sequence of characters, or a sequence ASCII codes from a given atom. To do this, we will again use atom_chars() and atom_codes() predicates but now, the first argument is an atom and the second argument is a variable. While atom_chars() decomposes atom to characters, atom_codes() decomposes them to…

  • Constructing Atoms

    Constructing Atoms

    Atom constructing means forming individual atom(s). We get a single atom from a list of characters and multiple atoms a list of ASCII values. atom_chars() constructs atom from characters and atom_codes() construct atoms from ASCII values. In both the predicates, the first argument will be one variable, and the second argument will be a list.

  • Handling Input and Output

    Handling Input and Output

    Till now, we have executed queries on the console. But many-a-times, it is necessary to some information from the user, process it and then display the result on the console. For such applications, we must know how to perform input-output operations in Prolog. The write() Predicate As the name suggests, the write() predicate takes the parameter as input, and…

  • Miscellaneous Programs

    Miscellaneous Programs

    Program 1: Check whether the list has odd number of elements or the even number of elements. To implement this functionality, we will define predicates list_even_len(L) and list_odd_len(L) while considering the following points. Write the following rules in kb5.pl and execute the commands as shown below. Credit: Copyright (C) 1999-2021 Daniel Diaz Program 2: Find the maximum…

  • Shift Operation

    Shift Operation

    Shift operation is used to shift one element of a list to the left rotationally. For example, given a list [a,b,c,d], then after shifting, it will be [b,c,d,a]. To implement shifting operation in Prolog, copy the following rule in kb5.pl and execute the instructions as given below. The clause list_shift(L1, L2) takes an input list…

  • Reverse Operation

    Reverse Operation

    The reverse operation arranges the items of a list in reverse order. For example, a list L = [a,b,c,d,e] when reversed will give output as [e,d,c,b,a]. To create a predicate, list_reverse(List, ReversedList), we must remember that, Add the instructions to implement list_rev predicate in kb5.pl and check the execution in console as shown in figure.

  • Append into List

    Append into List

    While appending two lists, we add two lists or add one list as an element of the other list. If the element to be added is already present in the list, then the append function simply returns. Before writing instructions to implement list_append(L1, L2, L3) predicate, note that, If A is an element to be…

  • Delete from List

    Delete from List

    To delete an element X from a list L, one of the three cases may arise. We need to add the following instructions for the predicate list_delete in the kb5.pl and run the commands as shown in figure.

  • Concatenation

    Concatenation

    Concatenating two lists’ means adding the elements of the second list after the first one. For example, if we have a list with elements [a,b,c,d] and another list with elements [e,f,g,h], then the concatenated list will have elements [a,b,c,d,e,f,g,h]. To implement list_concat predicate in Prolog, add the following lines in kb5.pl file and execute the…

  • Length Calculation

    Length Calculation

    This is used to find the length of list L. We will define one predicate to do this task. Suppose the predicate name is list_length(L,N). This takes L and N as input argument. This will count the elements in a list L and instantiate N to their number. As was the case with our previous…