Add mod, gcd, and hypot; term_expansion for math ops.
This commit is contained in:
parent
ae769d2341
commit
206f49598e
|
|
@ -9,7 +9,7 @@ average == [sum 1.0 *] [size] cleave /
|
||||||
b == [i] dip i
|
b == [i] dip i
|
||||||
binary == unary popd
|
binary == unary popd
|
||||||
ccons == cons cons
|
ccons == cons cons
|
||||||
cleave == fork [popd] dip
|
cleave == fork popdd
|
||||||
codireco == cons dip rest cons
|
codireco == cons dip rest cons
|
||||||
dinfrirst == dip infrst
|
dinfrirst == dip infrst
|
||||||
disenstacken == ? [uncons ?] loop pop
|
disenstacken == ? [uncons ?] loop pop
|
||||||
|
|
@ -22,7 +22,9 @@ enstacken == stack [clear] dip
|
||||||
flatten == [] swap [concat] step
|
flatten == [] swap [concat] step
|
||||||
fork == [i] app2
|
fork == [i] app2
|
||||||
fourth == rest third
|
fourth == rest third
|
||||||
|
gcd == true [tuck mod dup 0 >] loop pop
|
||||||
grba == [stack popd] dip
|
grba == [stack popd] dip
|
||||||
|
hypot == [sqr] ii + sqrt
|
||||||
ifte == [nullary] dipd swap branch
|
ifte == [nullary] dipd swap branch
|
||||||
ii == [dip] dupdip i
|
ii == [dip] dupdip i
|
||||||
infra == swons swaack [i] dip swaack
|
infra == swons swaack [i] dip swaack
|
||||||
|
|
|
||||||
17
thun/thun.pl
17
thun/thun.pl
|
|
@ -44,6 +44,14 @@ term_expansion(comparison_operator(X), (func(X, [A, B|S], [C|S]) :-
|
||||||
term_expansion(comparison_operator(X, Y), (func(X, [A, B|S], [C|S]) :-
|
term_expansion(comparison_operator(X, Y), (func(X, [A, B|S], [C|S]) :-
|
||||||
F =.. [Y, B, A], catch((F -> C=true ; C=false), _, C=F))).
|
F =.. [Y, B, A], catch((F -> C=true ; C=false), _, C=F))).
|
||||||
|
|
||||||
|
% Likewise for math operators, try to evaluate, otherwise use the symbolic form.
|
||||||
|
|
||||||
|
term_expansion(math_operator(X), (func(X, [A, B|S], [C|S]) :-
|
||||||
|
F =.. [X, B, A], catch(C is F, _, C=F))).
|
||||||
|
|
||||||
|
term_expansion(math_operator(X, Y), (func(X, [A, B|S], [C|S]) :-
|
||||||
|
F =.. [Y, B, A], catch(C is F, _, C=F))).
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
An entry point.
|
An entry point.
|
||||||
|
|
@ -128,10 +136,11 @@ func(dup, [A|S], [A, A|S]).
|
||||||
func(pop, [_|S], S ).
|
func(pop, [_|S], S ).
|
||||||
|
|
||||||
% Symbolic math. Compute the answer, or derivative, or whatever, later.
|
% Symbolic math. Compute the answer, or derivative, or whatever, later.
|
||||||
func(+, [A, B|S], [B + A|S]).
|
math_operator(+).
|
||||||
func(-, [A, B|S], [B - A|S]).
|
math_operator(-).
|
||||||
func(*, [A, B|S], [B * A|S]).
|
math_operator(*).
|
||||||
func(/, [A, B|S], [B / A|S]).
|
math_operator(/).
|
||||||
|
math_operator(mod).
|
||||||
|
|
||||||
% Attempt to calculate the value of a symbolic math expression.
|
% Attempt to calculate the value of a symbolic math expression.
|
||||||
func(calc, [A|S], [B|S]) :- B is A.
|
func(calc, [A|S], [B|S]) :- B is A.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue