// Abstract ------------------------------------------------------------------------------------------------------------------------------

This sample is a simple 4 operations calculator (which can deal with a single variable term).

// Examples ------------------------------------------------------------------------------------------------------------------------------

?- #calc([1,add,5],:r)
-> ( 6 ) := 1.00 (0.001) 1
?- #calc([1,add,[3,mul,10]],:r)
-> ( 31 ) := 1.00 (0.003) 1
?- #calc([[5,mul,:x],mul,13],130)
-> ( 2 ) := 1.00 (0.004) 1
?- #calc([[5,mul,2],mul,:v],130)
-> ( 13 ) := 1.00 (0.006) 1
?- #calc([[5,mul,2],mul,[1,add,:v]],130)
-> ( 12 ) := 1.00 (0.010) 1
 
// Code ----------------------------------------------------------------------------------------------------------------------------------

calc {

    ops = [add,sub,mul,div] // the supported operations

} {

    // deal with empty list and number
    ([],0)^         :- true;
    (:v,:v)         :- is.number(:v)^;

    // deal with unknown operation
    ([_,:o,_],0)    :- peek(ops,:ops), !lst.member(:o,:ops)^, console.puts("sorry ",:o," isn't a supported operand.");

    // deal with left or right operands being variables
    ([:l,:o,:r],:v) :- is.variable(:l)^, !is.variable(:r), #calc(:r,:rv), fun.make(:o,[:l,:rv,:v],:f),
                       #fzz.eval([primitive],:f);
    ([:l,:o,:r],:v) :- is.variable(:r)^, !is.variable(:l), #calc(:l,:lv), fun.make(:o,[:lv,:r,:v],:f),
                       #fzz.eval([primitive],:f);

    // deal with left or right operands being non-final
    ([:l,:o,:r],:v) :- !is.final(:l)^, is.final(:r), is.number(:v), #calc(:r,:rv), fun.make(:o,[:lv,:rv,:v],:f),
                       #fzz.eval([primitive],:f), #calc(:l,:lv);
    ([:l,:o,:r],:v) :- !is.final(:r)^, is.final(:l), is.number(:v), #calc(:l,:lv), fun.make(:o,[:lv,:rv,:v],:f),
                       #fzz.eval([primitive],:f), #calc(:r,:rv);

    // main prototype
    ([:l,:o,:r],:v) :- #calc(:l,:lv), #calc(:r,:rv), fun.make(:o,[:lv,:rv,:v],:f), #fzz.eval([primitive],:f);

}

// ---------------------------------------------------------------------------------------------------------------------------------------

[Home] [Email] [Twitter] [LinkedIn]