两个变量集的联合

时间:2014-12-10 01:35:37

标签: prolog iso-prolog

鉴于两个变量列表,ISO Prolog中最紧凑和规范的方法是什么来确定两者的结合?也就是说,我们需要(元逻辑)谓词的定义

varset_union(VarSet1, VarSet2, Union)

以及列表清单

varset_union(VarSets, Union)

其中Union是给定VarSet的唯一变量列表。

以下是ISO / IEC 13211-1:1995中的overview of the built-ins,包括Cor.2:2012。

2 个答案:

答案 0 :(得分:13)

使用term_variables/2的解决方案:

varset_union(VarSet1, VarSet2, Union):-
    term_variables([VarSet1|VarSet2], Union).

varset_union(VarSets, Union):-
    term_variables(VarSets, Union).

使用setof/3的解决方案:

varset_union(VarSet1, Varset2, Union):-
    varset_union([VarSet1, VarSet2], Union).

varset_union([], []).
varset_union(VarSets, Union):-
    setof(Var, VarSet^(member(VarSet, VarSets), member(Var, VarSet)), Union).

答案 1 :(得分:2)

基于Tudor的好答案,我设计了一个 <html> <head> <script type="text/javascript"> function checkunit() { if(document.getElementById("unit2").value=="CK") { document.getElementById("unittype1").innerHTML='cm'; document.getElementById("unittype2").innerHTML='cm'; document.getElementById("unittype3").innerHTML='cm'; } else { document.getElementById("unittype1").innerHTML='in'; document.getElementById("unittype2").innerHTML='in'; document.getElementById("unittype3").innerHTML='in'; } } function validatecal(form) { if (form.vm_length.value=="" || form.vm_length.value==0) {alert('Enter Length'); form.vm_length.focus(); return false} if (!IsNumber(form.vm_length.value) && form.vm_length.value!="") {alert('Invalid Length'); form.vm_length.focus(); return false} if (form.vm_width.value=="" || form.vm_width.value==0) {alert('Enter Width'); form.vm_width.focus(); return false} if (!IsNumber(form.vm_width.value) && form.vm_width.value!="") {alert('Invalid Width'); form.vm_width.focus(); return false} if (form.vm_height.value=="" || form.vm_height.value==0) {alert('Enter Height'); form.vm_height.focus(); return false} if (!IsNumber(form.vm_height.value) && form.vm_height.value!="") {alert('Invalid Height'); form.vm_height.focus(); return false} calcVolWeight(); return false; } function calcVolWeight() { var lengthValue = document.getElementById("vm_length").value; var widthValue = document.getElementById("vm_width").value; var heightValue = document.getElementById("vm_height").value; var weightDivisor = document.getElementById("unit2").value == "IP" ? 139 : 5000; var roundFactor = document.getElementById("unit2").value == "IP" ? 1 : 0.5; var originalVolume = lengthValue * widthValue * heightValue; // Calculate the volumetric weight var calcVolWeight = roundFactor * Math.ceil(( originalVolume / weightDivisor ) / roundFactor); document.getElementById("total_weight").value = parseFloat(calcVolWeight); } </script> </head> <body> <form name="vcalculator" id="vcalculator" method="post" onSubmit="return validatecal(this)"> <label >Select Units :</label> <select name="unit2" id="unit2" onChange="checkunit()" > <option value="CK">CM/KG</option> <option value="IP">Inches/Pounds</option> </select> </div> </div> <label >Enter Dimensions :</label> <label ">Length</label> <input type="text" name="vm_length" id="vm_length" > <span id="unittype1" >cm</span> <label ">Width</label> <input type="text" name="vm_width" id="vm_width" /> <span id="unittype2" >cm</span> <label ">Height</label> <input type="text" name="vm_height" id="vm_height" /> <span id="unittype3" >cm</span> <label >Dimensional Weight :</label> <input type="text"disabled="disabled"name="total_weight" id="total_weight" > <input type="submit" value="Calculate" name="Submit"/>&nbsp; <input type="reset" value="Clear" name="Submit2" /> </form> </body> </html> 的定义更紧凑 2个字符:

varset_union/3

- )