Re: N00b question on let
- From: "Kaz Kylheku" <kkylheku@xxxxxxxxx>
- Date: 28 Nov 2006 23:19:09 -0800
Ken Tilton wrote:
Pillsy wrote:
Ken Tilton wrote:
Actually, if this were documented it would make sense, since
a consequence of exponentiation's higher precedence is that it cannot be
applied to a signed number and discarding the sign might be a useful
convenience for EXPT to provide.
EXPT doesn't do that, though.
* (expt -2 3)
-8
Good point.
But also if x is -3, then (expt x 2) is 9, just like x^9 in some other
notation. Whereas you want (expt -3 2) where there is no variable
substitution to be -9.
It could be done like this. Suppose that the leading - were to be a
notation which causes -X to expand to (- X), just like 'X means (QUOTE
X). Then EXPT could be a special form which checks for the (- ...)
syntax and treats (expt (- X) Y) as (- (expt X Y)).
So if the negative sign is in the value, then it's exponentiated along
with the value. If it's in the syntax, it's factored out as lower
precedence.
Problem solved! Or, should we say, awful bullshit introduced! :)
But if you had this unary minus read macro, you would just use it like
this:
-(expt 3 2)
This would be understood as being the equivalent of -3^2, and
(expt -3 2)
which, being (expt (- 3) 2) would be serve as the equivalent (-3)^2.
So there would be no need for the special form hacking. And in fact
there would be no real need for the unary minus read macro either.
Everyone could just use the minus function directly: (- (expt 3 2)) or
(expt (- 3) 2) just like in other languages where unary minus is an
operator. The key is that we banished the sign from numeric constants.
You don't have a problem with (expt (- 3) 2) being nine, right?
Basically, what you are pretending to have a problem with is that Lisp
numeric symbols have support for negation built into them. But this is
very useful, particularly when Lisp expressions are used as data. What
should the printed representation of a negative number be? If you print
negative forty-two as (- 42), you have an issue. When scanned back,
should this be converted---at read time!---to an integer atom
representing negative forty-two? Or should it stay as a list
expression, making the program deal with it when it's reading numeric
data?
In a language whose code does not serve as data, only the compiler or
interpreter has the privilege of processing expressions, and those
represent program code. That compiler or interpreter hunts down the
unary minuses and folds them to constants, as part of its translation
scheme. It's a processing step that would have to be repeated
everywhere, if that language were opened up to its programs as a data
and code representation.
Lastly, think about the stupid issues caused by supporting negative
numbers with a unary operator in some languages, like ANSI C.
Two's complement representations have an extra negative value. E.g. in
an implementation where int is 16 bits, the available range is from
-32768 to 32767.
This means that in C on a two's complement platform, you cannot
freaking write the most negative number directly. Because -32768 means
apply the unary minus operator to 32768, and 32768 is out of range.
Like, doh, know what I mean? The Lisp approach here would eliminate
that problem, because the tokenizer would just recognize -32768 and
produce the corresponding value. So in a Lisp with range-limited
integers, you'd just have straightforward (defconstant *int-min*
-32768).
In a C compiler, if you want to define INT_MIN in <limits.h>, you have
to do some convolutions like:
#define INT_MAX 2147483647 /* 32 bit impl */
#define INT_MIN (-INT_MAX-1)
Try searching for "#define INT_MIN (-INT_MAX-1)" on
codesearch.google.com.
There is no hope for the beast. I say we deprecate the damn
thing and have done with it.
But Kenny, in programming languages, there tends to be a big, huge
disconnect between deprecating something and actually being done with
it.
.
- References:
- Re: N00b question on let
- From: Ken Tilton
- Re: N00b question on let
- Prev by Date: Re: philip greenspun, why list has nothing to show for 40 years of work and lispers stuck in .net or unix(java?)
- Next by Date: Re: philip greenspun, why list has nothing to show for 40 years of work and lispers stuck in .net or unix(java?)
- Previous by thread: Re: N00b question on let
- Next by thread: Re: N00b question on let
- Index(es):
Relevant Pages
|