diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2017-08-05 21:11:30 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2017-08-05 21:11:30 -0700 |
commit | 2844bb73b485450660d70de2de489590d0995d9e (patch) | |
tree | c13cce580f6a983db3db2086e141917b7b645cb6 | |
parent | 905a08374e2901e97b18e58d970f95a25ec6fc10 (diff) | |
download | txr-2844bb73b485450660d70de2de489590d0995d9e.tar.gz txr-2844bb73b485450660d70de2de489590d0995d9e.tar.bz2 txr-2844bb73b485450660d70de2de489590d0995d9e.zip |
Add sum and prod convenience functions.
* eval.c (eval_init): prod and sum intrinsics registered.
* lib.c (sum, prod): New functions.
* lib.h (sum, prod): Declared.
* txr.1: Documented.
-rw-r--r-- | eval.c | 2 | ||||
-rw-r--r-- | lib.c | 12 | ||||
-rw-r--r-- | lib.h | 2 | ||||
-rw-r--r-- | txr.1 | 44 |
4 files changed, 60 insertions, 0 deletions
@@ -5829,6 +5829,8 @@ void eval_init(void) reg_fun(plus_s = intern(lit("+"), user_package), func_n0v(plusv)); reg_fun(intern(lit("-"), user_package), func_n1v(minusv)); reg_fun(intern(lit("*"), user_package), func_n0v(mulv)); + reg_fun(intern(lit("sum"), user_package), func_n1(sum)); + reg_fun(intern(lit("prod"), user_package), func_n1(prod)); reg_fun(intern(lit("abs"), user_package), func_n1(abso)); reg_fun(intern(lit("trunc"), user_package), func_n2o(trunc, 1)); reg_fun(intern(lit("mod"), user_package), func_n2(mod)); @@ -3277,6 +3277,18 @@ val numneqv(struct args *args) return t; } +val sum(val seq) +{ + args_decl_list(args, ARGS_MIN, tolist(seq)); + return plusv(args); +} + +val prod(val seq) +{ + args_decl_list(args, ARGS_MIN, tolist(seq)); + return mulv(args); +} + val max2(val a, val b) { return if3(less(a, b), b, a); @@ -691,6 +691,8 @@ val gev(val first, struct args *rest); val lev(val first, struct args *rest); val numeqv(val first, struct args *rest); val numneqv(struct args *list); +val sum(val seq); +val prod(val seq); val max2(val a, val b); val min2(val a, val b); val maxv(val first, struct args *rest); @@ -33472,6 +33472,50 @@ follows, then that value is divided by that subsequent divisor. This process repeats until all divisors are exhausted, and the value of the last division is returned. +.coNP Functions @ sum and @ prod +.synb +.mets (sum << num-sequence ) +.mets (prod << num-sequence ) +.syne +.desc +The +.code sum +and +.code prod +functions operate on a single argument +.metn num-sequence , +which is a sequence of numbers. + +The +.code sum +function returns the left-associative sum of the elements of +.meta num-sequence +calculated as if using the +.code + +function. Similarly, the +.code prod +function calculates the left-associative product of the elements of +.metn num-sequence , +as if using the +.code * +function. + +If +.meta num-sequence +is empty then +.code sum +returns +.code 0 +and +.code prod +returns +.codn 1 . + +If +.meta num-sequence +contains one number, then both functions +return that number. + .coNP Functions @ wrap and @ wrap* .synb .mets (wrap < start < end << number ) |