Introduction
Wrapping your mind around the concepts of Logical Programming is no easy feat--even more so than Functional Programming. This is especially true if you've already grown accustomed to thinking imperatively.
But once the concepts sink in you'll find yourself discovering time and again how much cleverer some procedural solution would be if it were done in Prolog.
Prolog is extremely useful in the realm of Artificial Intelligence.
Examples
I'm going to be taking a class in A.I. this semester so I will have lots of prolog examples from my class homework assignments...
Sorting A List Of Numbers
Needless to say there's a million A.I. applications where sorting a list would be useful. I coded up this sorting routine today during my "Short Fiction" class...hehe.
mySort( TASKS, TASKS ) :- length( TASKS, 1 ).
mySort( IN, [ H | [ HT | TT ] ] ) :-
select( H, IN, RIN ),
member( HT, RIN ),
H @=< HT,
mySort( RIN, [ HT | TT ] ).say I had the list [3,2,1,4,6,5,7,9,8] and I wanted it sorted. I would call
mySort([3,2,1,4,6,5,7,9,8],Answer).
and the answer would then be accessible in the variable "Answer".
Note this implementation is miserably inefficient, but the goal here is education and not efficiency.
Links
[^http://www.swi-prolog.org/ HomePage of SWI Prolog]
