• Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r4wp

[#Red] Red language group

Oldes
17-Nov-2012
[3845x2]
In zero-based REBOL it would not be possible to write:
for i 1 length? b 1 [print i]
but instead:
for i 0 (-1 + length? b) 1 [print i]
On the other side, here is quite nicely explained, why zero-based 
is better http://programmers.stackexchange.com/questions/110804/why-are-zero-based-arrays-the-norm
Pekr
17-Nov-2012
[3847x8]
Oldes - don't care - zero does not exist :-)
why 1 would be a better starting index than zero? Why not 2, or 10? 
The answer itself is interesting because it shows a lot about the 
though process of the people defending the idea.

 - what an flawed argument - the guy completly missess language as 
 REBOL imo, where we have first, second, etc. And first simply returns 
 first real thing. What is zeroth thing?
Well, really difficult to settle, any way you think abou the topic. 
I can think about 0 as about in-between, non real value - like Max 
mentioned vectors, simply first element in series to the left, or 
to the right. Then I can think about 0 as about real value - if I 
look outside, there is -1C temperature. And in order to get to 1C 
temperature, 2 grades are needed, hence 0 is real value here. And 
finally - if I will have some real series in front of me, e.g. 10 
ppl, I can pick first, second, first to the right, first to the left 
(-1), hence no zero or negative index here ...
I will leave it for the more clever to decide :-)
I am not sure REBOL introduced the easy model - it is mixture of 
those real existing values, and virtual elements:

blk: [a b c]
index? head blk == 1
index? tail blk == 4


I can imagine tail simply pointing to the last element, as well as 
head pointng to the "zeroth" element. But then, if we would not think 
about "in-between" positions, how would insert behave? If I would 
once agin think about the real example with e.g. 3 ppl standing in 
the line, and I say - go, and insert yourself to the last position 
- does it mean new person is going to be third, and shifts the last 
one to the fourth position, or does it mean the person just adds 
himself/herself to the fourth position?
prof.dr. Edsger W. Dijkstra - why numbering should start at 0 - http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html
In the short article, there's also some experience with early programming 
languages ...
btw - will Red have range! datatype? And would it influence the way 
we look into series, positioning, indices, etc?
Arnold
17-Nov-2012
[3855]
Completely agreed 0 does not exist, even not for computers. A computer 
with 0 memory does not have memory at address 0 either :)

Counting starting at 0 is nonsense. No matter who and how many times 
it is explained. In human/fysic world we only put letter 1 in envelope 
1, letter 2 in envelope 2 etcetera, there is no letter 0 to put into 
envelope 0. It is only a confusing trick to start at 0. (I know you 
can look at memory like a binary tree) In these days of plenty of 
memory, I say let location 0 unused. Besides for who was REBOL meant 
initially? People. Scientists not computerscientists. Let those struggle 
with C and the likes. 1-base is one of the big plusses REBOL has 
over the majority of other languages. (enough bikeshedding from me 
today)
Pekr
17-Nov-2012
[3856]
Yet those ppl use arguments, like even in Bibble, there was zeroth 
day (well, in physics, that might mean the singularity point, where 
our physic laws don't apply), or, that without 0 you are not able 
to count BC to nowadays difference properly. Whoever came with that 
BC AD distinction, was not sane at the first place, and I really 
doubt, that back at those times, they agreed that their age is some 
-2300 years, knowing the point 0 (Christ) ahead :-)
Andreas
17-Nov-2012
[3857]
Please move the discussion whether "0 exists" elsewhere. REBOL's 
integer! corresponds to integers and includes 0. Unless you want 
to change that as well, that's a fact we have to live with.
DocKimbel
17-Nov-2012
[3858]
Default base index in programming languages: 

http://en.wikipedia.org/wiki/Comparison_of_programming_languages_%28array%29#Array%5Fsystem%5Fcross-reference%5Flist


Notable languages with 1-based indexing: FORTRAN, Lua, Mathematica, 
MATLAB, Smalltalk.
Pekr
17-Nov-2012
[3859]
Andreas - stop pesking us for such a chat. Many arguments areound 
are based upon various arguments, some of them leading to history 
of mankind/math itself. Thanks ...
Andreas
17-Nov-2012
[3860]
Pekr, please chat elsewhere.
DocKimbel
17-Nov-2012
[3861]
Range! datatype: I would like to add one, but it needs preliminary 
study on how it can affect series semantics.
PeterWood
17-Nov-2012
[3862x2]
Whilst Pascal allows arrays to have a user specified base for array 
indeces, the default is 1 based. It also allows a zero element:

Code
program Arrays;
uses SysUtils;
var
  i : Integer;
  myArray : Array[-1..1] of Integer = (1,2,3); 
begin
  for i := -1 to 1 do

    writeln('Element ' + IntToStr(i) + ' Value ' + IntToStr(myArray[i]));
end.

Result
Element -1 Value 1
Element 0 Value 2
Element 1 Value 3
An interesting point of view on zero v 1 based indexing - http://alarmingdevelopment.org/?p=470
Andreas
17-Nov-2012
[3864x2]
One very important point is, I think, that this discussion is not 
necessarily about 0-based vs 1-based.


It is more about if and how to map the concept of ordinals to the 
concept of integers. If you choose to use indices-as-ordinals ("1-based 
indices"), those two questions collapse to one. If you choose to 
use indices-as-offsets ("0-based indices"), the question of how to 
handle ordinals _still remains_.
We not only have to think about the use case of "give me the first 
element", but also about "give me the element 2 steps after that 
other element" or "how many elements are in between two other elements".
PeterWood
17-Nov-2012
[3866x4]
One reason I posted the Pascal example was due to the earlier discussion 
about  ordinals not including zero. In Pascal, integer are considered 
an ordinal type and include zero. Now the Pascal definition of ordinal 
may not be absolutely correct in mathematical terms but I would suggest 
it is pragmatic to support reasonable behaviour for indexing.
I don't see quite the same distinction as you between indices-as-ordinals 
and indices-as-offsets except in terms of implementation where the 
former requires additional steps in the calculation compared with 
the latterr. (Though as you say this is not the issue).
So am I correct in that the issue is more the mixing of absolute 
indexing with  relative indexing?

== "123456789"
>> index? ser      
== 1
>> ser: next ser   
== "23456789"
>> index? ser   
== 2
>> pick ser 1   
== #"2"
So would two sets of functions, one providing absolute addressing 
and the other providing relative addressing solve the issue?
Andreas
17-Nov-2012
[3870x2]
Not really. You would still need to clarify the semantics for how 
the relative addressing should work.
If it is allowed to use integers in relative addressing, you will 
have to define what ought to happen for non-positive integers.
PeterWood
17-Nov-2012
[3872]
As in 

>> pick ser -1
== #"1"
Andreas
17-Nov-2012
[3873]
And `pick ser 0`, yes.
PeterWood
17-Nov-2012
[3874]
Which I guess is back wehre the discussion started.
Andreas
17-Nov-2012
[3875]
Exactly :)
PeterWood
17-Nov-2012
[3876]
Well at least I understand the issue a little better :-)
Andreas
17-Nov-2012
[3877]
Ada and VHDL/Verilog are two other languages that spring to mind, 
which allow choosing the index range, like Pascal.
PeterWood
17-Nov-2012
[3878]
But I guess they are all absolute addressing rather than addressing 
realtive to a current position? I believe Pascal is.
Andreas
17-Nov-2012
[3879]
One thing I always find interesting about definable range of Pascal, 
is that Wirth later moved on to fixed, 0-based ranges for arrays 
in Oberon.
PeterWood
17-Nov-2012
[3880x3]
Yes I noticed that.
Is it true to say that whilst there is no effective difference (in 
logical terms) between 0-based and 1-based absolute indexing, there 
is a difference when it comes to relative indexing,
The difference being how to handle zero.
Andreas
17-Nov-2012
[3883]
Basically that's fair to say, yes.
PeterWood
17-Nov-2012
[3884x2]
I feel that there is a consitent way to look at 1-based relative 
addressing but only if you consider 0 to be the position immediately 
before the current position.


It is to define the given index as an offset from 1 (the current 
position). (Examples using current position as absolute index 5 [1-based])


index 10 would be calculated as offset 9 positions from the current 
position [absolute 14]

index 1 would be calculated as offset 0 from the current position 
[absolute 5]
index 0 wold be offest -1 from the current position [absolute 4]

index -1 would be offset - 2 from the current position [absolute 
3]


I know that most people will see this as a kludge or don't like it's 
different behaviour to REBOL2.
On the other hand, it can be explained in a consistent manner.
Andreas
17-Nov-2012
[3886]
Yes, that's also R3's behaviour.
PeterWood
17-Nov-2012
[3887]
Back to the start again? :-)
DocKimbel
17-Nov-2012
[3888]
Here is a new proposition for solving the "PICK issue":

1) Forbid <= 0 indexes.


2) Add a PICK-BACK action that will take only positive integers, 
PICK-BACK series 1 would return the first element on the left of 
current series position.


3) As PICK-BACK is not a very nice and concise name (I like actions 
to be named with a single word), an op! alternative would be provided: 
<- (opening angle-bracket followed by a dash aka "ASCII arrow")

Examples:
    >> series: next [A B C]
    >> pick-back series 1
    == A
    >> series <- 1
    == A
    >> series <- 2
    == none			;-- to be consistent with out-of-bound PICK


For path notations options in such scenario, I'm open to propositions. 
So what are the pros/cons of such solution?
PeterWood
17-Nov-2012
[3889x2]
In forbidding indeces < 1 would none be returned or a runtime error 
raised?
Seeing as the use cases for accessing elements before the 'HEAD appears 
to be quite rare, couldn't pick-back be left as a "mezzanine":

pick-back: func [ser index] [pick skip ser negate index 1]
DocKimbel
17-Nov-2012
[3891x2]
A runtime error. Having it as a mezz could be a good option.
Having it as an action would make it much faster when interpreted 
instead of compiled.
PeterWood
17-Nov-2012
[3893]
As for naming wouldn't a refinement be better?

pick/back series pos-int
DocKimbel
17-Nov-2012
[3894]
Maybe.