compvalue(expression,dataset,ruleset) → expression
The subroutine compvalue takes as argument a term, a dataset, and a ruleset. If tries to compute a value for the specified term using the facts in the specified dataset and the rules in the specified ruleset. If it succeeds, it returns the resulting value; otherwise, it returns false.
The most common use of compvalue is to evaluate compound terms involving predefined functions.
Call: compvalue(read('plus(2,3)'), repository, library)
Exit: 5
Call: compvalue(read('max(plus(2,3),times(2,3))'), repository, library)
Exit: 6
Call: compvalue(read('stringify(art)'), repository, library)
Exit: "art"
Call: compvalue(read('stringappend("Hello",", ","World")'), repository, library)
Exit: "Hello, World"
Call: compvalue(read('sum([2,3,4])'), repository, library)
Exit: 9
Call: compvalue(read('map(stringify,[art,bob,cal])'), repository, library)
Exit: ["art","bob","cal"]
If the specified term is a symbol, the symbol is returned as value. When called on a variable, compvalue always returns false.
Call: compvalue('23', repository, library)
Exit: 23
Call: compvalue('art', repository, library)
Exit: art
Call: compvalue('X', repository, library)
Exit: false
If the specified term is compound and the operator is not predefined and does not have a function definition, then it is treated as a constructor. In this case, the constructor is applied to the values of the constituent terms and the result returned as value.
Call: grind(compvalue(read('f(a,b)'), repository, library))
Exit: f(a,b)
Call: grind(compvalue(read('f(plus(2,3),times(2,3))'), repository, library))
Exit: f(5,6)
In the case of compound terms in which the operator is not predefined but has a matching definition, the arguments are evaluated and the definition is applied to the resulting values.
Call: definerules(library,readdata('quad(X) := times(X,X)'))
Exit: ...
Call: grind(compvalue(read('quad(3)'), repository, library))
Exit: 9
Call: grind(compvalue(read('quad(plus(2,3))'), repository, library))
Exit: 25
|