Subject: Re: IF & AND redefined, why?
From: Erik Naggum <erik@naggum.net>
Date: 18 Apr 2001 11:11:23 +0000
Newsgroups: comp.lang.lisp
Message-ID: <3196581083601514@naggum.net>

* Ted Sandler <tedsandler@worldnet.att.net>
> e.g.  the following code has to compute the function "calculate-answer"
> twice:
> 
> (if (calculate-answer)
>     (format t "The answer is ~A" (calculate-answer))
>   (format t "There is no answer"))
> 
> The "myif" macro sought to avoid this by setting "it" to the result of
> the conditional as in:
> 
> (myif (calculate-answer)
>       (format t "The answer is ~A" it)
>   (format t "There is no answer"))

  Programmers with slightly more experience would probably write this:

(format t "~:[There is no answer~;The answer is ~:*~A~]" (calculate-answer))

  Similar options are frequently available.

  Incidentally, you could name your anaphoric variable $_ or whatever Perl
  uses for the last value computed, and avoid the problem that it might
  look as if it were a tasteful thing to do.  "it" is not a reserved word,
  but it becomes one in myif and myand, and that's even worse than using a
  disgusting-looking Perlism.  If you really, really want anaphoric if and
  and, at least allow the programmer to choose the variable name, as in 

(iflet ((with-it (calculate-answer))
   (whatever with-it)
   (whatever-else)))

  Note: this is intentionally syntactically like a binding, so as not to
  confuse readers to believe that with-it is a function call.  This also
  makes it possible to roll this into a normal if.  (Provided precautions
  are taken against misinterpreting lamda forms, which are syntactically
  different from the binding form even in the one really pathological case.)

  And don't forget the functional style:

((lambda (x) (if x (whatever x) (whatever-else))) (calculate-answer))

#:Erik
-- 
  I found no peace in solitude.
  I found no chaos in catastrophe.
			-- :wumpscut: