RU beehive logo ITEC dept promo banner
ITEC 380
2009fall
ibarland

homeinfolecturesexamshwsarchive

lect10c
prolog intro


; Lists in prolog:
;  Scheme:       Prolog:
;    empty         []
;    cons           .
;    list          [ ..,..,..]
;    cons*         [ ..,..,.. | .. ]


; For example:
mem_v2(_,[]) :- not(true).
mem_v2(D,.(D,_)) :- true.
mem_v2(D,.(_,R)) :- mem(D,R).


% We can simplify this syntax,
% once we internalize Prolog's mind-set:

mem(X,[X|_]).
mem(X,[_|R]) :- mem(X,R).






% Other functions to write:
% atLast(X,L)
% removeLast(L1,L2)
% reverse(L,R)
% append (L1,L2,L)
% zip(L1,L2,L)
%   e.g.  zip( [a,b,c], [d,e,f], [a,d,b,e,c,f] ).

Numbers in Prolog

To do arithmetic (rather than pattern-matching), we use a keyword: “is”:

Var is Expr
However, there is a caveat: all variables in Expr have to already have been bound, at the time when this constraint is evaluated! Here are some examples:

    % A version of 'reverse' which requires 'removeLast' and 'last':
    reverse([],[]).
    reverse([F|R],L2) :- last(L2,F), removeLast(L2,L2less), reverse(R,L2less).
    
    %  removeLast, Last -- not included in notes, 
    %  since it's part of hw05  !!
    
    
    
    %% Alternate version: An accumulator variable:
    reverse(L1,L2) :- reverseHelp( L1, [], L2 ).
    
    %%  [+,+,-]
    %% reverseHelp(L1,L2,L3): 
    %% True when: L1 reversed, appended to L2, equals L3.
    %    First, a less idiomatic solution:
    % reverseHelp([],RevSoFar,Soln) :- RevSoFar = Soln.
    % reverseHelp(Lst, RevSoFar, Soln) :-
    %    Lst = [Frst,Rst],
    %    reverseHelp( Rst, [Frst,RevSoFar], Soln ).
    
    % Here is the same thing, in idiomatic Prolog:
    % We just move the "=" clauses into the definitions:
    %
    reverseHelp([], RevSoFar, RevSoFar).
    reverseHelp([Frst|Rst], RevSoFar, Out) :- 
        reverseHelp(Rst, [Frst|RevSoFar], Out).
    
    
    %% [+,+,-]
    %% filterSmallerThan( Threshold, Lst, Result ):
    %% True when 'Result' is all the elements of 'Lst' which
    %% are less than Threshold.
    
    %    First, a less idiomatic solution:
    % filterLessThan(Threshold,Lst,Answer) :- Lst=[], Answer=[].
    % filterLessThan(Threshold,Lst,Answer) :- 
    %     Lst = [Frst,Rst],
    %     Frst >= Threshold, 
    %     filterLessThan(Threshold,Rst,SmallRest),
    %     Answer = SmallRest.
    % filterLessThan(Threshold,Lst,Answer) :- 
    %     Lst = [Frst,Rst],
    %     Frst < Threshold, 
    %     filterLessThan(Threshold,Rst,SmallRest),
    %     Answer = [Frst|SmallRest].
    
    % Here is the same thing, in idiomatic Prolog:
    % We just move the "=" clauses into the definitions:
    %
    filterLessThan(Threshold,[],[]).
    filterLessThan(Threshold,[Frst|Rst],SmallRest) :- 
          Frst >= Threshold, 
          filterLessThan(Threshold,Rst,SmallRest).
    filterLessThan(Threshold,[Frst|Rst],[F|SmallRest]) :- 
          Frst < Threshold, 
          filterLessThan(Threshold,Rst,SmallRest).
    
    
nth(0,[F|_],F).
nth(I,[_|R],Answer) :-
  Iless1 is I-1,
  nth(Iless1,R,Answer).
 - briefly show a tracing of the search (w/o worrying 'bout the search alg)
 - talk/reflect about how you can view parameters as input or output params.
 - facts *are* just rules w/o variables.

If time:
 - singleton variables; using '_' for 'don't-care'
 - an example on processing a list: I do some, they do some:
       removeOnce( Target, Data, Result )   (search)
       contains(Target, Data)               (search)
       isAllEvens(Data)                     (search)
       filterEvens(Data,Result)             (filter)
       squareEach(Data,Result)              (map)
       sum(Data,Total)                      (fold)
 - talk/reflect about matching of sub-lists, in variables.  Write 'third'.
   (how cool is that?!
    Cool enough that other languages copied it: e.g.  http://www.radford.edu/itec380/2009fall-ibarland/Lectures/matching.ss )

homeinfolecturesexamshwsarchive


©2009, Ian Barland, Radford University
Last modified 2009.Nov.06 (Fri)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme