home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / b / binprolog / !BinPro330 / progs / bestof < prev    next >
Encoding:
Text File  |  1993-11-23  |  1.1 KB  |  43 lines

  1. go:-G=max([3,4.66,1,-0.5],_Max),G,write(G),nl.
  2.  
  3. max(Xs,X):-bestof(X, >, member(X,Xs)).
  4.  
  5. % The following is an efficient implementation
  6. % of bestof/3 using the blackboard.
  7. % You add it to extra.pl and type ?-boot. to integrate it in the kernel
  8. % if you plan to use it.
  9.  
  10. % true if X is an answer of Generator such that
  11. % X Rel Y for every other answer Y of Generator
  12. bestof(X,Closure,Generator):-
  13.   copy_term(X,Y),
  14.   Closure=..L1,
  15.   det_append(L1,[X,Y],L2),
  16.   Test=..L2,
  17.   bestof0(X,Y,Generator,Test).
  18.  
  19. bestof0(X,Y,Generator,Test):-
  20.   inc_level(bestof,Level),
  21.   Generator,
  22.   update_bestof(Level,X,Y,Test),
  23.   fail.
  24. bestof0(X,_,_,_):-
  25.   dec_level(bestof,Level),
  26.   val(bestof,Level,X),
  27.   rm(bestof,Level).
  28.  
  29. % uses Rel to compare New with so far the best answer
  30. update_bestof(Level,New,Old,Test):-
  31.   val(bestof,Level,Old),!,
  32.   Test,
  33.   bb_set(bestof,Level,New).
  34. update_bestof(Level,New,_,_):-
  35.   bb_let(bestof,Level,New).
  36.  
  37. % ensure correct implementation of embedded calls to bestof/3
  38. inc_level(Obj,X1):-val(Obj,Obj,X),!,X1 is X+1,bb_set(Obj,Obj,X1).
  39. inc_level(Obj,1):-bb_def(Obj,Obj,1).
  40.  
  41. dec_level(Obj,X):-val(Obj,Obj,X),X>0,X1 is X-1,bb_set(Obj,Obj,X1).
  42.  
  43.