tisp

tiny lisp
git clone git://edryd.org/tisp
Log | Files | Refs | LICENSE

commit 344c615a40eb3bba52082842d100ff450bce7d7b
parent 5f56cf6faeadf5522b16cd807aeda73f4f477ff8
Author: Ed van Bruggen <edvb@uw.edu>
Date:   Sat, 16 Mar 2019 13:05:13 -0700

Add log and exponential functions

Diffstat:
test.c | 1+
tibs/lib.tsp | 13+++++++++++++
tibs/math.c | 7+++++--
tibs/repl.tsp | 12++++++------
4 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/test.c b/test.c @@ -282,6 +282,7 @@ main(void) int errors[LEN(tests)] = {0}; Env env = tisp_env_init(64); tib_env_math(env); + tisp_eval(env, tisp_parse_file(env, "tibs/lib.tsp")); for (int i = 0; ; i++) { if (!tests[i][1]) { diff --git a/tibs/lib.tsp b/tibs/lib.tsp @@ -61,4 +61,17 @@ (define (not x) (cond (x ()) (t t))) +(define e (exp 1.)) +(define (root b p) (pow b (/ 1 p))) +(define (sqrt x) (root x 2)) +(define (cbrt x) (root x 3)) +(define (logb b x) (/ (log x) (log b))) +(define (log10 x) (logb 10. x)) +(define (positive? x) (cond ((> x 0) t) (t ()))) +(define (negative? x) (cond ((< x 0) t) (t ()))) + +(define (! n) + (cond ((= n 1) 1) + (t (* n (! (- n 1)))))) + (define (disp x) (print x (newline))) diff --git a/tibs/math.c b/tibs/math.c @@ -229,12 +229,13 @@ PRIM_TRIG(atan) PRIM_TRIG(asinh) PRIM_TRIG(acosh) PRIM_TRIG(atanh) +PRIM_TRIG(exp) +PRIM_TRIG(log) void tib_env_math(Env env) { - tisp_env_add(env, "pi", mk_dec(3.141592653589793)); - tisp_env_add(env, "e", mk_dec(2.718281828459045)); + tisp_env_add(env, "pi", mk_dec(3.141592653589793)); tsp_env_fn(numerator); tsp_env_fn(denominator); @@ -266,4 +267,6 @@ tib_env_math(Env env) tsp_env_name_fn(arcsinh, asinh); tsp_env_name_fn(arccosh, acosh); tsp_env_name_fn(arctanh, atanh); + tsp_env_fn(exp); + tsp_env_fn(log); } diff --git a/tibs/repl.tsp b/tibs/repl.tsp @@ -1,12 +1,12 @@ (define (repl) (print "> ") - ((lambda (exp) - (cond ((= exp 'quit) (print (newline))) + ((lambda (expr) + (cond ((= expr 'quit) (print (newline))) (t (begin - ((lambda (eval-exp) - (cond ((not (void? eval-exp)) - (disp eval-exp)))) - (eval exp)) + ((lambda (eval-expr) + (cond ((not (void? eval-expr)) + (disp eval-expr)))) + (eval expr)) (repl))))) (read)))