From 6cd6af985619569e7e12df7b28c6aa1dc10726c1 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 6 Nov 2014 06:35:34 -0800 Subject: Allow then-func to be omitted in iff. Cleanup. * eval.c (eval_init): Register iff as requiring only one arg. * lib.c (do_iff): Do not check thenfun for nil. Just call it. (iff): Default thenfun to identity. * txr.1: Documentation updated. --- ChangeLog | 11 +++++++++++ eval.c | 2 +- lib.c | 3 ++- txr.1 | 53 +++++++++++++++++++++++++++++++++++++---------------- 4 files changed, 51 insertions(+), 18 deletions(-) diff --git a/ChangeLog b/ChangeLog index 97693d32..523d9f74 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2014-11-06 Kaz Kylheku + + Allow then-func to be omitted in iff. Cleanup. + + * eval.c (eval_init): Register iff as requiring only one arg. + + * lib.c (do_iff): Do not check thenfun for nil. Just call it. + (iff): Default thenfun to identity. + + * txr.1: Documentation updated. + 2014-11-05 Kaz Kylheku * lib.c (iff): Bugfix: forgotten handling of default diff --git a/eval.c b/eval.c index aab5a683..402d19a2 100644 --- a/eval.c +++ b/eval.c @@ -3749,7 +3749,7 @@ void eval_init(void) reg_fun(intern(lit("andf"), user_package), func_n0v(andv)); reg_fun(intern(lit("orf"), user_package), func_n0v(orv)); reg_fun(intern(lit("notf"), user_package), func_n1(notf)); - reg_fun(intern(lit("iff"), user_package), func_n3o(iff, 2)); + reg_fun(intern(lit("iff"), user_package), func_n3o(iff, 1)); reg_fun(intern(lit("iffi"), user_package), func_n3o(iffi, 2)); reg_fun(intern(lit("if"), user_package), func_n3o(if_fun, 2)); reg_fun(intern(lit("or"), user_package), func_n0v(or_fun)); diff --git a/lib.c b/lib.c index 4674ff26..6abbf1f1 100644 --- a/lib.c +++ b/lib.c @@ -4606,12 +4606,13 @@ static val do_iff(val env, val args) cons_bind (thenfun, elsefun, choices); return if3(apply(condfun, args, nil), - if2(thenfun, apply(thenfun, args, nil)), + apply(thenfun, args, nil), if2(elsefun, apply(elsefun, args, nil))); } val iff(val condfun, val thenfun, val elsefun) { + thenfun = default_arg(thenfun, identity_f); elsefun = default_bool_arg(elsefun); return func_f0v(cons(condfun, cons(thenfun, elsefun)), do_iff); } diff --git a/txr.1 b/txr.1 index 165848b1..2ca3a0a1 100644 --- a/txr.1 +++ b/txr.1 @@ -20628,7 +20628,7 @@ and then inverts the result as if by application of the .coNP Functions @ iff and @ iffi .synb -.mets (iff < cond-func < then-func <> [ else-func ]) +.mets (iff < cond-func >> [ then-func <> [ else-func ]]) .mets (iffi < cond-func < then-func <> [ else-func ]) .syne .desc @@ -20639,7 +20639,7 @@ function is the functional equivalent of the operator. It accepts functional arguments and returns a function. -The resulting function takes its arguments and applies them to +The resulting function takes its arguments, if any, and applies them to .metn cond-func . If .meta cond-func @@ -20650,6 +20650,15 @@ resulting value is returned. Otherwise the arguments are passed to .meta else-func and the resulting value is returned. +If +.meta then-func +is omitted then +.code identity +is used as default. This omission is not permitted by +.codn iffi , +only +.codn iff . + If .meta else-func needs to be called, but is omitted, then @@ -20663,26 +20672,38 @@ function differs from only in the defaulting behavior with respect to the .meta else-func -argument. The following equivalences hold: +argument. If +.meta else-func +is omitted in a call to +.code iffi +then the deafult function is +.codn identity . +This is useful in situations when one value is to be +replaced with another one when the condition is true, otherwise +preserved. + +The following equivalences hold between +.code iffi +and +.codn iff : .cblk - (iffi a b c) <--> (iff a b c) + (iffi a b c) <--> (iff a b c) - (iffi a b) <--> (iff a b identity) + (iffi a b) <--> (iff a b identity) - (iffi a b nil) <--> (iff a b identity) + (iffi a b false) <--> (iff a b) + + (iffi a identity false) <--> (iff a) .cble -The -.code iffi -function defaults to the identity function when -.meta else-func -is -omitted or -.codn nil , -and therefore is useful in situations when one value is to be -replaced with another one when the condition is true, otherwise -preserved. +The following equivalence illustrates +.code iff +with both optional arguments omitted: + +.cblk + (iff a) <---> (iff a identity false) +.cble .coNP Functions @ tf and @ nilf .synb -- cgit v1.2.3