Send Close Add comments: (status displays here)
Got it!  This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
Prolog: Iteration and loops


1. Prolog: Iteration and loops

2. Iteration and loops
In functional and logic languages, the concept of a machine state changed by a destructive update is not in the core language.

Most functional and logic languages provide some way to change state in a limited way but that is not the focus here.

3. Repetition using loops
Here is a function in JavaScript that prints numbers in a range from first parameter to (but not including) the second parameter. A while loop is used in the function.

Here is the JavaScript code.

Here is the output of the JavaScript code.


4. Repetition using recursion
Here is a function in JavaScript that prints numbers in a range from first parameter to (but not including) the second parameter. A recursive function is used.

Here is the JavaScript code.

Here is the output of the JavaScript code.


5. Comparison
In both of the above examples, a divide and conquer problem solving strategy is used whereby the problem of size n is divided into a problem of size 1 (one) and a problem of size n-1.

The loop version is not as obvious.

This becomes clearer from an expansion of the while loop.

6. While loop
Consider the following while loop (pseudo code) form.
While b Do    S    End While

The above form is equivalent to the following form.
If b Then    S    While b Do       S       End While    End If

This process is called loop unrolling as it can be continued until a fixed point is reached.

7. Prolog
In imperative and functional languages a function takes parameters and returns a value.

In Prolog (and logic languages) there is not the same concept of a function.

The concept is more of a procedure but the procedures are called predicates.

8. Logic
In logic, there are the following areas. In logic terms, Prolog is Turing Complete in that it can model both propositional and predicate calculus.

9. Iteration in Prolog
Here is the Prolog code.

Here is the output of the Prolog code.


10. Call by value
Most imperative programming languages have a parameter passing mechanism called "call by value" in which an actual parameter is evaluated (using eager evaluation) to a value and a copy of that value is passed.

The original actual parameter cannot be changed.

11. Call by reference
The "call by reference" mechanism passes a reference which allows the called routine to change the original actual parameter.

12. Call by pattern matching
Logic languages such as Prolog (and lazy functional languages) have a parameter passing mechanism that can be described as "call by pattern".

That is, a pattern, or logical variable (placeholder to be filled in later, but only once) is passed and the called routine performs pattern matching on the called actual parameter (structure, etc.). The following rule succeeds only if I1 unifies with I2.
prange(I1, I2):- I1 == I2.

But this can be shortened using pattern matching to the following.
prange(I1,I1).


13. End of page