home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l217 / 2.ddi / PROGRAMS / MIXTURE.PRO < prev    next >
Encoding:
Prolog Source  |  1990-03-26  |  3.0 KB  |  116 lines

  1. /************************************************************************/
  2. /*                                                                      */
  3. /*                  PDC Prolog example program                          */
  4. /*                                    */
  5. /*   Copyright (c) 1986, 90 by Prolog Development Center                */
  6. /*                                                                      */
  7. /*        A couple of small usefull predicates            */
  8. /*                                                                      */
  9. /************************************************************************/
  10.  
  11. DOMAINS
  12.   LIST=INTEGER*
  13.  
  14. PREDICATES
  15.   append(LIST,LIST,LIST)
  16.   member(INTEGER,LIST)
  17.   reverse(LIST,LIST)
  18.   genl(LIST,INTEGER)
  19.   
  20.   repeat
  21.   timer
  22.  
  23.   plus(INTEGER,INTEGER,INTEGER)
  24.   numb(INTEGER)
  25.  
  26.   fib(INTEGER,INTEGER)
  27.   factorial(INTEGER,REAL)
  28.  
  29. CLAUSES
  30.   /* Concatenate two lists */
  31.   append([],L,L).
  32.   append([X|L1],L2,[X|L3]):-append(L1,L2,L3).  
  33.  
  34.   /* Membership of a list */
  35.   member(X,[X|_]).
  36.   member(X,[_|L]):-member(X,L).
  37.  
  38.   /* reverse a list */
  39.   reverse([],[]).
  40.   reverse([H|T],Lr):-reverse(T,Tr), append(Tr,[H],Lr).
  41.  
  42.   /* generate a reversed list */
  43.   genl( [], 0 ).
  44.   genl( [I|T], I )  :- I>0, I1=I-1, genl(T,I1).
  45.  
  46.   repeat. repeat:-repeat.
  47.  
  48.   timer:-
  49.     time(H1,M1,S1,D1),
  50.     /* Call program which should be timed */
  51.     time(H2,M2,S2,D2),
  52.     Time=(D2-D1)+100*( (S2-S1) + 60*( (M2-M1) +60*(H2-H1) ) ),
  53.     write("Time = ",Time,"/100 Sec" ),nl.
  54.  
  55.   /* This predicate implements addition for all flow-patterns */
  56.   plus(X,Y,Z):-bound(X),bound(Y),Z=X+Y.
  57.   plus(X,Y,Z):-bound(Z),bound(Y),X=Z-Y.
  58.   plus(X,Y,Z):-bound(Z),bound(X),Y=Z-X.
  59.   plus(X,Y,Z):-free(X),free(Y),bound(Z),numb(X),Y=Z-X.
  60.   plus(X,Y,Z):-free(X),free(Z),bound(Y),numb(X),Z=X+Y.
  61.   plus(X,Y,Z):-free(Y),free(Z),bound(X),numb(Y),Z=X+Y.
  62.   plus(X,Y,Z):-free(X),free(Y),free(Z),numb(X),numb(Y),Z=X+Y.
  63.  
  64.   numb(0).
  65.   numb(X):-numb(A), X=A+1.
  66.  
  67.   fib(1,1):-!.
  68.   fib(2,1):-!.
  69.   fib(I,RES):-I>2, I1=I-1, I2=I-2, fib(I1,M), fib(I2,N), RES=N+M.
  70.  
  71.   factorial(0,1):-!.
  72.   factorial(N,RES) :-
  73.     N1=N-1, factorial(N1,N1fak), Res=N*N1fak.
  74.  
  75.   
  76. PREDICATES
  77.   nondeterm for(Integer,Integer,Integer)
  78.   next(integer,integer)
  79.  
  80. CLAUSES
  81.   for(Cur,_,Cur).
  82.   for(Cur,Max,I):- Cur2=Cur+1, Cur2<=Max, for(Cur2,Max,I).
  83.    
  84.   next(Max,Max).
  85.   
  86. /* GOAL
  87.     MaxI=3,
  88.     MaxJ=5,
  89.     for(0,MaxI,I),nl,
  90.         writef("I= % : ",I),
  91.         for(1,MaxJ,J),
  92.             write("  J = ",J),
  93.         next(MaxJ,J),
  94.     next(MaxI,I).   
  95.  */   
  96.  
  97. PREDICATES
  98.     sort(LIST,LIST)
  99.     sort(LIST,LIST,LIST)
  100.     split(LIST,INTEGER,LIST,LIST)
  101.  
  102. CLAUSES
  103.     sort(LIST,RESULT):-
  104.           sort(LIST,[],RESULT).    
  105.     sort([HEAD|TAIL],SORTEDBIGGER,RESULT):-
  106.           split(TAIL,HEAD,LESSLIST,BIGGERLIST),
  107.           sort(BIGGERLIST,SORTEDBIGGER,NEWSORTEDBIGGER),
  108.           sort(LESSLIST,[HEAD|NEWSORTEDBIGGER],RESULT).
  109.     sort([],SORTED,SORTED).
  110.  
  111.     split([],_,[],[]).
  112.     split([Y|L],X,[Y|L1],L2):-Y<X,!,split(L,X,L1,L2).
  113.     split([Y|L],X,L1,[Y|L2]):-split(L,X,L1,L2).
  114.     
  115.     
  116.