Re: Why equal(x[n], x[n..n])=0 ?
- Posted by Jason Gade <jaygade at yaho?.?om> Sep 24, 2007
- 711 views
Fernando Bauer wrote: > > Hi Derek! Thanks for your reply. > > Derek Parnell wrote: > > > > Fernando Bauer wrote: > > > > > > Hi All, > > > > > > I was debugging a function when I noticed that a slice with equal indexes > > > is > > > different from an access with one index. x[n] is different from x[n..n] > > > where > > > n is a valid index. > > > According to the manual, a slice always result in a sequence (also when > > > x[n] > > > is an atom). But, in particular, when we have something like x[n..n], the > > > result > > > is x[n] with the its depth incremented by 1. In other words, the structure > > > of > > > the element depends on the access form (subscription or slicing). For me, > > > this > > > is a surprising fact. > > > This kind of implementation affects some algorithms, because we have to > > > test > > > when the indexes are equal in order to not use slice. > > > So, why equal(x[n], x[n..n])=0 ? > > > > Because the syntax [n..m] is a slice which is a sequence - always. And the > > syntax > > [n] is a element reference whose value depends in what is at location 'n'. > > Specifically, > > the syntax [n..n] is a sequence which has a length of one. > > > > Given the sequence (string) s = "qwerty", then s[3..3] is the string "e" and > > s[3] is the character 'e'. And characters are not strings. > > > > Another way to look at is this ... > > > > s[3 .. 4] is "er" > > s[3 .. 3] is "e" > Ok. But why s[3..3] is not 'e'? Why the original 'e' is transformed in "e" ? > > > s[3 .. 2] is "" > > > > so making s[3..3] ("e") mean the same as s[3] ('e') is inconsistent. > Why do you think this is inconsistent? I could think the opposite. Why does > the slice s[3..3] change the type of s[3]? I thought that it was already adequately explained, but let me try again. s[3..3] is *not* the same as s[3]. s[3] is an element of a sequence, which may be a sequence or an atom. s[3..3] is a sequence with the elements from index 3 to index 3 (which happen to be the same index). A slice is *always* a sequence. See http://www.rapideuphoria.com/refman_2.htm#1 section 2.2.6. With your idea, how would s[3..2] work? What is it supposed to return? Currently it returns the empty sequence. -- A complex system that works is invariably found to have evolved from a simple system that works. --John Gall's 15th law of Systemantics. "Premature optimization is the root of all evil in programming." --C.A.R. Hoare j.