Tuesday, March 24, 2015

Useful Tips From My First Week Of Prolog:


  1. Understanding how Prolog works is paradigm. Unlike other procedural languages, Prolog is seeking to set things equal, or unify them. This a much more intelligent behavior that characterizes it as a “fifth-generation language”. The philosophy of the fifth-generation languages is to attempt to “describe” the problem to Prolog as opposed to just writing it out. Take that with a grain of salt though. It will make sense as you develop more and more advanced programs.
  2. Prolog is pretty bare bones in that you have to write a lot of basic features yourself, and you also have limited tools at your disposal for debugging. One of the tools you need to take advantage of is the trace function. This allows you incite to the complex control flow of Prolog’s stack. However, do not get too caught up in what it’s trying to do, but instead focus on what is being called, and watch diligently for failures.
  3. When making accumulator-style predicates, try to include an extra argument, as it will make writing a base case a lot easier. Here’s a simple example of an accumulator-style length predicate, len, that calls on a predicate accLen to accomplish the task:

    accLen([_|T],A,L)  :-
    Anew  is  A+1,  
    accLen(T,Anew,L).
    accLen([],A,A).    
    len(List,Length)  :-  accLen(List,0,Length).
  4. If you have a problem where prolog seems to want to “redo” one of your predicates to find another unification, you can use the symbol ! to denote no redos. This will be most helpful in your base case.
  5. Prolog has a sort of “not” operator, but it can be hard to find in the documentation or in a google search (unless you sold out to SWI-prolog). By using \+ we signify that the predicate is true if it fails.

    For example,
    \+ member(x, List).

    Is only true if x is not in the list.

No comments:

Post a Comment