Re: Why equal(x[n], x[n..n])=0 ?

new topic     » goto parent     » topic index » view thread      » older message » newer message

Fernando Bauer wrote:

> > 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" ?

It seems that you are not thinking things through clearly. The character 'e' has
not been transformed.

The bucket analogy is an excellent one. 

If you have a bucket of fruit, and you want to create a NEW bucket of fruit
using one piece of fruit from the initial one, the syntax to do that is

    newBucket = oldBucket[n..n]

If you have a bucket of fruit, and you want to look at one piece of fruit that
is in it, the syntax to do that is

    theFruit = oldBucket[n]

As you can see, the syntax you actually use depends on what you want to achieve.
The slice syntax [x..y] gives you a new sequence/container/bucket, and the
element reference syntax [z] gives you one element from a container.

> Yes. The manual is also very clear about this. However, 
> the meaning of my question is another:
> Why does Euphoria (the manual) define slices 
> like x[n..n] and x[n] differently?

You misunderstand, it seems. 

   [n..n] IS a slice, and [n] IS NOT a slice. 
   [n..n] IS NOT an element reference, and [n] IS an element reference.

> Why is an atom transformed in a sequence by using a slice?

There is no transformation. If one has a sequence (which a slice always is), one
can insert an atom into it. The atom is not transformed.

> An analogy:
> Suppose you have a set of enumerated objects and the following rules:
> a)You can hold only one at a time. (subscription)

Hold the object in what? A container maybe?

> b)If you need more objects you can use a container. (slicing)
> Then, if someone asks you to collect the objects 2 to 4?
>  - Because the number of objects is 3, you need to use
> the container (rule 2).

Why? That is only one option. Another is to hold the VALUES in a set of objects.

    object a,b,c
    a = bucket[2]
    b = bucket[3]
    c = bucket[4]

This is why I was saying that the way you want to see the data dictates the
method used to store the data.

> Now, if someone asks you to collect the objects 2 to 2?
>  - Because the number of objects is 1, you *don't* need
> to use the container, you can catch it directly (rule 1). 

So you acknowledge this is optional. That is, you can have the single object as
an entity itself or you can has it as the contents of a container. It's your
choice. So to tell Euphoria which method you have chosen, you use the appropriate
syntax. If you want the just the object use bucket[2], however if you want it in
a container then use bucket[2..2]. It's your choice.

> Besides, if you use the container (sequence),you will 
> have to discard it to catch the object (an unnecessary procedure).

Why? It depends on what you want to do with it. It's your choice.

-- 
Derek Parnell
Melbourne, Australia
Skype name: derek.j.parnell

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu