RE: A question about certain language features
- Posted by bensler at mail.com
Mar 22, 2002
While it is possible to write foo[1..0], I'd be baffled to see it in
someone's code. You could easily write {} or "" instead.
The entire implementation of shorthand slicing breaks down to 1 item:
length(s)
My logic is that '0' is the only element not currently usable, so
naturally it should be used to represent the length of s (not 'end of
sequence').
Imagine a sequence s, in EU's memory with a length of 5.
I know this isn't how sequences work, but looking from the outside,
in...
s[5][1][2][3][4][5]
0 1 2 3 4 5
When you break it down to the single factor: length(s),
s[0] makes plenty of sense to me.
When I was writing EUPP, I tossed around the idea of using a symbol
instead of '0' for the same reason that Derek said, it's prone to being
typed unintentionally. When I tried other symbols, I felt like I was
having to learn something new, whereas it seemed natural to use '0'. I
also found that '0' was easier for me to discern than other symbols.
I was also not enthusiastic about introducing a new symbol where an
existing symbol would work and seemed natural.
I tried using no symbol, and was distraught with the lack of
readability. The symbols serve as separators as well as designators.
without the end symbol, or without the start symbol, it was hard to tell
where one slice ended and another began. The time spent trying to read
the code outwieghed the time spent writing it.
The best alternative to '0' that I came up with was '?'. It's the most
readable symbol that can be used, it doesn't add any new symbols to the
language, and it goes with the shorthand already implemented for
print().
Instead of thinking of ? as a shorthand for print(), it would instead be
thought of as just a shorthand. I suspect there are other places in EU's
syntax where this could be put to use as well.
To add to the benefit of using ? we could have:
?? as a shorthand for printing strings
I don't particularly care what symbol is used, but I think there are
benefits to one over another. I know Derek's point, and the logic to
using '0' is fairly obscure :P
That's why I suggest ? instead.
Someone might argue that ? is already used as a symbol in EU. Only in
one specific case, and it's barely utilized. I'd rather have an old dog
with a new trick, than a new dog with a new trick. Instead of 2 dogs
with two tricks, I have one dog with 2 tricks.
I've noticed that people have the mindset of the symbol representing the
'END' of the sequence. I prefer to think of the symbol as representing
the 'LENGTH' of the sequence. Just semantics, but it makes more sense to
me, seeing as we are only talking about the outer dimension of a
sequence, and the actual code that would be replaced is length().
Chris
Mike Nelson wrote:
> foo[1..0] is already a legal (null) slice, using it to mean length(foo)
> will
> break existing code. Perhaps a better scheme would be using -1 for the
> end
> of the sequence. This is also fairly intuitive:
>
> foo[1..3] means the first thru third elements conting from the
> beginning,
> and
> foo[-3..-1] would mean the third thru first elements counting from the
> end.
>
> also something like foo[2..-2] would be legal (this would return the
> sequence minus the first and last element)
>
> Internally a negative slice would be converted like this:
>
> foo[-3..-1] becomes foo[length(foo)+1-3,length(foo)=1-1] which can be
> simplifed in this case to foo[length(foo)-2..length(foo)]
>
> Of course, the same negative indexing should apply to subscripts, where
> it
> is even more intuitive:
>
> foo[4] is the fouth element form the beginning, foo[-4] is the fourth
> element from the end.
>
> -- Mike Nelson
>
>
|
Not Categorized, Please Help
|
|