1. append and &

Can someone explain the difference between these two statements
-----------------------------
sequence a
a = "a"
for i = 1 to 10
    a = append(a,"a")
end for
----------------------------
sequence a
a = "a"
for i = 1 to 10
    a = a & "a"
end for
----------------------------

I'm having some conceptual confusion with results from above as compared
with 'vectors', 'matrices' from prior languages.


george

new topic     » topic index » view message » categorize

2. Re: append and &

On Thu, 21 Mar 2002 06:43:31 -0500
George Walters <gwalters at sc.rr.com> wrote:

> 
> Can someone explain the difference between these two statements
> -----------------------------
> sequence a
> a = "a"
> for i = 1 to 10
>     a = append(a,"a")
> end for
> ----------------------------
> sequence a
> a = "a"
> for i = 1 to 10
>     a = a & "a"
> end for
> ----------------------------

Ignoring the fact that neither example works (you need a 'do' in there!:),
the first creates an 'array' of ten 1-character strings:   "a","a","a","a"......
while the second simply appends characters to the original string:
"aaaaaaaaaaa"

You can print the second as a string: puts(1,a), but you have to index into the
first before you can print an element.
print a[2] => "a"

More interesting would be:
a = "Ant"
a &= "Cat"
a &= "Dog"
a &= "Rat"
This does a standard string concatenation: puts(1,a) => "AntCatDogRat"

However: 
a = "Ant"
a = append(a,"Cat")
a = append(a,"Dog")
a = append(a,"Rat")

does NOT result in what I would expect: an array of 3 strings
["Ant","Cat","Dog","Rat"],
instead, it creates a string containing nested sub-strings:
["Ant{"Cat","Dog","Cat"]"],
so trying to print a[2] gives 'n', not "Cat" as expected. i.e:
a[1] => 'A'
a[2] => 'n'
a[3] => 't' 
-- ready for the surprise?
a[4] => "Cat"
a[5] => "Dog"....

To create an array of strings, you need to declare the first as follows:
a = {"Ant"} -- note the braces
a = append(a,"Cat")
a = append(a,"Dog")...

now, puts(1,a[2]) => "Cat"

The exact same results can be had from:
a = {"Ant"} 
a &= {"Cat"}
a &= {"Dog"} ...

Regards,
Irv

new topic     » goto parent     » topic index » view message » categorize

3. Re: append and &

----- Original Message -----
From: "George Walters" <gwalters at sc.rr.com>
To: "EUforum" <EUforum at topica.com>
Subject: append and &


>
> Can someone explain the difference between these two statements
> -----------------------------
> sequence a
> a = "a"
> for i = 1 to 10
>     a = append(a,"a")
> end for
> ----------------------------
> sequence a
> a = "a"
> for i = 1 to 10
>     a = a & "a"
> end for
> ----------------------------
>
> I'm having some conceptual confusion with results from above as compared
> with 'vectors', 'matrices' from prior languages.


append() always increases the result by exactly one element.

& always increases the results by the length of the source.

In both cases, atoms are considered to have a length of 1 for this exercise.

Thus a = append(b, c) always adds a single element, c, as a new element to
the end of b.
  eg:

    a = {1,2,3}
    a = append(a,a)
      ==> {1,2,3,{1,2,3}} The length increases by 1.
    a = append(a,a)
      ==> {1,2,3,{1,2,3},{1,2,3,{1,2,3}}} The length increases by 1.

And a = b & c always just joins c to the back of b, increasing the length by
how ever long c is.

    a = {1,2,3}
    a = a & a
      ==> {1,2,3,1,2,3} The length increases by 3.
    a = a & a
      ==> {1,2,3,1,2,3,1,2,3,1,2,3} The length increases by 6.

For sequences,  a = append(a,a) is equivalent to  a &= {a}

For atoms, a = append(a,a) is equivalent to a &= a.

-----------
Derek.

new topic     » goto parent     » topic index » view message » categorize

4. Re: append and &

nice tutorial, thanks... I've been getting mighty confused on indexing into
the tables i've been creating.


george
----- Original Message -----
From: "Irv Mullins" <irvm at ellijay.com>
To: "EUforum" <EUforum at topica.com>
Subject: Re: append and &


>
> On Thu, 21 Mar 2002 06:43:31 -0500
> George Walters <gwalters at sc.rr.com> wrote:
>
> >
> > Can someone explain the difference between these two statements
> > -----------------------------
> > sequence a
> > a = "a"
> > for i = 1 to 10
> >     a = append(a,"a")
> > end for
> > ----------------------------
> > sequence a
> > a = "a"
> > for i = 1 to 10
> >     a = a & "a"
> > end for
> > ----------------------------
>
> Ignoring the fact that neither example works (you need a 'do' in there!:),
> the first creates an 'array' of ten 1-character strings:
"a","a","a","a"......
> while the second simply appends characters to the original string:
> "aaaaaaaaaaa"
>
> You can print the second as a string: puts(1,a), but you have to index
into the first before you can print an element.
> print a[2] => "a"
>
> More interesting would be:
> a = "Ant"
> a &= "Cat"
> a &= "Dog"
> a &= "Rat"
> This does a standard string concatenation: puts(1,a) => "AntCatDogRat"
>
> However:
> a = "Ant"
> a = append(a,"Cat")
> a = append(a,"Dog")
> a = append(a,"Rat")
>
> does NOT result in what I would expect: an array of 3 strings
["Ant","Cat","Dog","Rat"],
> instead, it creates a string containing nested sub-strings:
> ["Ant{"Cat","Dog","Cat"]"],
> so trying to print a[2] gives 'n', not "Cat" as expected. i.e:
> a[1] => 'A'
> a[2] => 'n'
> a[3] => 't'
> -- ready for the surprise?
> a[4] => "Cat"
> a[5] => "Dog"....
>
> To create an array of strings, you need to declare the first as follows:
> a = {"Ant"} -- note the braces
> a = append(a,"Cat")
> a = append(a,"Dog")...
>
> now, puts(1,a[2]) => "Cat"
>
> The exact same results can be had from:
> a = {"Ant"}
> a &= {"Cat"}
> a &= {"Dog"} ...
>
> Regards,
> Irv
>
>
>
>

new topic     » goto parent     » topic index » view message » categorize

5. Re: append and &

thanks Derek....

george
----- Original Message -----
From: "Derek Parnell" <ddparnell at bigpond.com>
To: "EUforum" <EUforum at topica.com>
Subject: Re: append and &


>
> ----- Original Message -----
> From: "George Walters" <gwalters at sc.rr.com>
> To: "EUforum" <EUforum at topica.com>
> Sent: Thursday, March 21, 2002 10:43 PM
> Subject: append and &
>
>
> > Can someone explain the difference between these two statements
> > -----------------------------
> > sequence a
> > a = "a"
> > for i = 1 to 10
> >     a = append(a,"a")
> > end for
> > ----------------------------
> > sequence a
> > a = "a"
> > for i = 1 to 10
> >     a = a & "a"
> > end for
> > ----------------------------
> >
> > I'm having some conceptual confusion with results from above as compared
> > with 'vectors', 'matrices' from prior languages.
>
>
> append() always increases the result by exactly one element.
>
> & always increases the results by the length of the source.
>
> In both cases, atoms are considered to have a length of 1 for this
exercise.
>
> Thus a = append(b, c) always adds a single element, c, as a new element to
> the end of b.
>   eg:
>
>     a = {1,2,3}
>     a = append(a,a)
>       ==> {1,2,3,{1,2,3}} The length increases by 1.
>     a = append(a,a)
>       ==> {1,2,3,{1,2,3},{1,2,3,{1,2,3}}} The length increases by 1.
>
> And a = b & c always just joins c to the back of b, increasing the length
by
> how ever long c is.
>
>     a = {1,2,3}
>     a = a & a
>       ==> {1,2,3,1,2,3} The length increases by 3.
>     a = a & a
>       ==> {1,2,3,1,2,3,1,2,3,1,2,3} The length increases by 6.
>
> For sequences,  a = append(a,a) is equivalent to  a &= {a}
>
> For atoms, a = append(a,a) is equivalent to a &= a.
>
> -----------
> Derek.
>
>
>
>

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu