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.
Logic programming: more  Prolog examples


1. Logic programming: more  Prolog examples

2. More Prolog examples
Here are some more examples of Prolog programs.

3. Strings
Here is the Prolog code.

Here is the output of the Prolog code.


4. Factorial
The mathematical definition of factorial is defined as follows for non-negative integers.
   fact(0) = 1    fact(n) = n * fact(n-1), n > 0

Here is a Prolog implementation of the Factorial function.

Here is the Prolog code.

Here is the output of the Prolog code.


5. Base conversion
Here is the Prolog code.

Here is the output of the Prolog code.


6. Observations
Note that to do what might be simple in other imperative languages might be somewhat cumbersome (though straight-forward) in Prolog.

If a problem fits Prolog's computation and resolution model then Prolog is a good choice.

Otherwise, there is some extra effort involved.

7. Modularity
Prolog lacks modularity (e.g., modules, namespaces, etc.).

A Prolog program is typically a collection of flat (non-nested) definitions.

8. End of page