Advertisement
Guest User

Untitled

a guest
Nov 28th, 2015
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Oz 3.32 KB | None | 0 0
  1. declare
  2. fun {AbsoluteValue X}
  3.    if X<0 then ~X else X end
  4. end
  5.  
  6. %Renvoie la valeur absolue de la différence entre le nombre de true et de false pour une question dans la base de donnée
  7. declare
  8. fun {CountTrueFalse L Q}
  9.    local CountTrueFalseAux in
  10.       fun {CountTrueFalseAux L Q Acc}
  11.      case L of
  12.         nil then {AbsoluteValue Acc}
  13.      []H|T then
  14.         if L.1.Q == true then {CountTrueFalseAux L.2 Q Acc+1}
  15.         else {CountTrueFalseAux L.2 Q Acc-1}
  16.         end
  17.      end
  18.       end
  19.       {CountTrueFalseAux L Q 0}
  20.    end
  21. end
  22.  
  23. %Retourne la question la plus rentable de toutes
  24. declare
  25. fun {OptimalQuestion L}
  26.    local OptimalQuestionAux in
  27.       %LQ : list de questions (accumulateur)
  28.       %Acc : rentabilité de la question la plus rentable POUR LE MOMENT
  29.       %Acc2 : question la plus rentable POUR LE MOMENT (c'est ce qu'on renvoie)
  30.       fun {OptimalQuestionAux L Acc Acc2 LQ}
  31.      case LQ of
  32.         nil then Acc2
  33.      [] H|T then if {AbsoluteValue {CountTrueFalse L H}}<Acc then {OptimalQuestionAux L {AbsoluteValue {CountTrueFalse L H}} H T}
  34.              else {OptimalQuestionAux L Acc Acc2 T}
  35.              end
  36.          end
  37.       end
  38.       {OptimalQuestionAux L {Length L} {Arity L.1}.1 {Arity L.1}}
  39.    end  
  40. end
  41.  
  42. %{Browse {OptimalQuestion DB}}
  43. %{Browse {CountTrueFalse DB 'Est-il blanc de peau ?'}}
  44.  
  45. %Retire une question Q chez tous les joueurs de la liste L
  46. declare
  47. fun {RemoveQuestion L Q}
  48.    local RemoveQuestionAux in
  49.       fun{RemoveQuestionAux L Q L2}
  50.         case L
  51.         of H|T then {RemoveQuestionAux T Q {Append L2 [{Record.subtract H Q}]}}
  52.         else L2
  53.      end
  54.       end
  55.       {RemoveQuestionAux L Q nil}
  56.    end
  57. end
  58.  
  59. % Retire le personnage P de la liste des joueurs L
  60. declare
  61. fun {RemovePerson P L}
  62.    local RemovePersonAux in
  63.       fun {RemovePersonAux P L Lnew}
  64.      case L of H|T then
  65.         if H.1==P then  {Append Lnew T}
  66.         else {RemovePersonAux P T {Append Lnew [H]}} end
  67.      else Lnew
  68.      end
  69.       end
  70.       {RemovePersonAux P L nil}
  71.    end
  72. end
  73.  
  74. %Crée une liste avec toutes les personnes ayant répondu A à une question Q
  75. declare
  76. fun {ListPersonTF L Q A}
  77.    local ListPersonTFAux in
  78.       fun{ListPersonTFAux L Q A Lnew}
  79.      case L of H|T then
  80.         if H.Q==A  then {ListPersonTFAux T Q A {Append Lnew [H]}}
  81.         else {ListPersonTFAux T Q A Lnew} end
  82.      else Lnew
  83.      end
  84.       end
  85.       {ListPersonTFAux L Q A nil}
  86.    end
  87. end
  88.  
  89. %Crée une liste avec toutes les personnes de la base de donnée DB
  90. declare
  91. fun {Persons DB}
  92.    fun {PersonsAux DB L}
  93.       case DB of nil then L
  94.       []H|T then {PersonsAux T H.1|L}
  95.       end
  96.    end
  97. in {PersonsAux DB nil}
  98. end
  99.  
  100. % Renvoie la liste de question où on a retiré Q
  101. declare
  102. fun {RemoveQ LQ Q}
  103.    fun {RemoveQAux LQ Q LQAux}
  104.       case LQ of nil then LQAux
  105.       []H|T then if H==Q then {Append LQAux T}
  106.          else {RemoveQAux T Q {Append LQAux [H]}}
  107.          end
  108.       end
  109.    end
  110. in {RemoveQAux LQ Q nil}
  111. end
  112.  
  113.  
  114. declare
  115. fun {BuildDecisionTree DB}
  116.    fun {Build DB LQ}
  117.       if DB==nil then leaf(nil)
  118.       elseif LQ==nil then leaf({Persons DB})
  119.       else
  120.      local Q in
  121.         Q = {OptimalQuestion DB}
  122.         question(Q
  123.              true:{Build {RemoveQuestion {ListPersonTF DB Q true} Q}  {Arity DB.1}.2}
  124.              false:{Build {RemoveQuestion {ListPersonTF DB Q false} Q} {Arity DB.1}.2})
  125.      end
  126.       end
  127.    end
  128. in {Build DB {Arity DB.1}.2}
  129. end
  130.  
  131. {Browse {BuildDecisionTree DB}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement