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 relations involving lists, it is useful to consider two cases −

  • If list is empty, then length is 0.
  • If the list is not empty, then L = [Head|Tail], then its length is 1 + length of Tail.

To find length of a list in Prolog, add the lines given below in kb5.pl file and then execute as shown in Figure.

images
images

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *