1. converting sequence to string

------=_NextPart_000_0005_01BF9760.33762AA0
        charset="iso-8859-2"
Content-Transfer-Encoding: quoted-printable

does anybody have the function that converts sequences with many other =
sequences under it, and sequences under it, into one sequence which has =
just atoms(=3Dstring)
i have tried to write such function but it would be quite complicated =
because there can be unlimited number of sequences under sequences...

------=_NextPart_000_0005_01BF9760.33762AA0
        charset="iso-8859-2"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-2" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.2614.3401" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>does anybody have the =
function&nbsp;that converts=20
sequences with many other sequences under it, and sequences under it, =
into one=20
sequence which has just atoms(=3Dstring)</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>i have tried to write&nbsp;such =
function but it=20
would be quite complicated because there can be unlimited number of =
sequences=20

------=_NextPart_000_0005_01BF9760.33762AA0--

new topic     » topic index » view message » categorize

2. Re: converting sequence to string

Škoda wrote:

> does anybody have the function that converts sequences with many other
> sequences under it, and sequences under it, into one sequence which
> has just atoms(=string)i have tried to write such function but it
> would be quite complicated because there can be unlimited number of
> sequences under sequences...

Yes! This is just screaming to be an application of one of my favourite
topics (more often known as programmers bane), Recursion!!! Yes, a
routine that calls itself (a simple one unfortunately). It's amazinh how
often a complex looking problem resolves itself when you try recursion.
I can see how this routine would be useful, especially (and this applies
to everyone) when you want to print a sequence with nested strings
(although you lose the nesting). That reminds me - would that be any use
to anyone? a puts that puts nested strings with the braces around them
to indicate where each one is? So something like this:
{Hello{How{Are you}?}This is an{example of}nested strings}, or perhaps
with commas before and after the braces.
Anyway, here's the code:

global function deNest(sequence nestedSequence)
        --deNests a sequence by recursing through it and 'flattening' it
to 1 level
        sequence outputSequence
        outputSequence = {}
        for a=1 to length(nestedSequence) do
                if not sequence(nestedSequence[a]) then
                        outputSequence = outputSequence &
nestedSequence[a]
                else
                        outputSequence = outputSequence &
deNest(nestedSequence[a])
                end if
        end for
        return outputSequence
end function

Enjoy!

Nick

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

3. Re: converting sequence to string

On Sun, 26 Mar 2000 19:34:48 +0000, Nick Johnson
<arachnid at MAD.SCIENTIST.COM> wrote:

>Yes! This is just screaming to be an application of one of my favourite
>topics (more often known as programmers bane), Recursion!!! Yes, a


  I don't think you can do recursion in Euphoria without the routine_id

  function or does your code work different ?

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

4. Re: converting sequence to string

----- Original Message -----
From: "Skoda" <tone.skoda at SIOL.NET>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Sunday, March 26, 2000 12:16 PM
Subject: converting sequence to string


>does anybody have the function that converts sequences with many other
sequences under >it, and sequences under it, into one sequence which has
just atoms(=string)
>i have tried to write such function but it would be quite complicated
because there can be >unlimited number of sequences under sequences...

Yeas, look in the archives for:

safeseq.e
or
PRINT.E   by Gabriel Boehme

But keep in mind a string is not a defined type in Eu, but you could modify
those .e files to return a sequence with all the nested sequences in the
original sequence unnested. That will sorta destroy all the array/record
relavance between the nested sequences though.

Kat

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

5. Re: converting sequence to string

On Sun, 26 Mar 2000, Bernie wrote:

>   I don't think you can do recursion in Euphoria without the routine_id
>   function or does your code work different ?

You can do recursion without routine_id, because the function is declared before
the line that calls it.

Routine_id is needed for what, in other languages, is called a "forward"
reference.

Regards,
Irv

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

6. Re: converting sequence to string

On Sun, 26 Mar 2000 17:58:43 -0500, Irv Mullins <irv at ELLIJAY.COM> wrote:

>You can do recursion without routine_id, because the function is declared
before
>the line that calls it.

  Irv:

This is the example I was replying to:
--
global function deNest(sequence nestedSequence)
--deNests a sequence by recursing through it and 'flattening' it
to 1 level
sequence outputSequence outputSequence = {}
for a=1 to length(nestedSequence) do
  if not sequence(nestedSequence[a]) then
    outputSequence = outputSequence&nestedSequence[a]
  else outputSequence = outputSequence &
    deNest(nestedSequence[a])    -- <<<<<<<<<<<<<<<<< --- look here
  end if
end for
return outputSequence
end function

  If I run this in Euphoria how will Euphoria know what deNest function
  looks like the first time in the first pass of the interpeter without
  seeing the whole function first thats why I thought that a routine_id
  function, a call_func function and routine_id variable is necessary.
  This needs a forward reference to resolve this code so a Euphoria does
  not error out on the first pass.
  Maybe I'am wrong so straight my thinking out ?
  Maybe you have a different trick to do this ?

  Bernie

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

7. Re: converting sequence to string

On Sun, 26 Mar 2000, you wrote:
> On Sun, 26 Mar 2000 17:58:43 -0500, Irv Mullins <irv at ELLIJAY.COM> wrote:
>
> >You can do recursion without routine_id, because the function is declared
> before
> >the line that calls it.
>
>   Irv:
>
> This is the example I was replying to:
> --
> global function deNest(sequence nestedSequence)
> -- deNests a sequence by recursing through it and 'flattening' it
> -- to 1 level
> sequence outputSequence outputSequence = {}
> for a=1 to length(nestedSequence) do
>   if not sequence(nestedSequence[a]) then
>     outputSequence = outputSequence&nestedSequence[a]
>   else outputSequence = outputSequence &
>     deNest(nestedSequence[a])    -- <<<<<<<<<<<<<<<<< --- look here
>   end if
> end for
> return outputSequence
> end function
>
>   If I run this in Euphoria how will Euphoria know what deNest function
>   looks like the first time in the first pass of the interpeter without
>   seeing the whole function first thats why I thought that a routine_id
>   function, a call_func function and routine_id variable is necessary.
>   This needs a forward reference to resolve this code so a Euphoria does
>   not error out on the first pass.
>   Maybe I'am wrong so straight my thinking out ?
>   Maybe you have a different trick to do this ?

No tricks. I just copied the routine above, sent a nested sequence:
s = {"One",{"Two","Three"},"Four"}
to it, and it returned:
"OneTwoThreeFour"

The reason it can work is because the function is declared before it is called.
It doesn't matter what code is inside the function...end function block,
Euphoria doesn't need to know that, just the fact that there _is_ a function
named deNest (and where it's located, I guess)

By the way, this is not a forward reference. The call to
deNest(nestedSequence(a)) is _after_ the declaration of the routine
by the same name.

As far as I know, code like the above will work even in languages which don't
support forward references.

Regards,
Irv

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

8. Re: converting sequence to string

Bernie Ryan wrote:

> On Sun, 26 Mar 2000 19:34:48 +0000, Nick Johnson
> <arachnid at MAD.SCIENTIST.COM> wrote:
>
> >Yes! This is just screaming to be an application of one of my favourite
> >topics (more often known as programmers bane), Recursion!!! Yes, a
>
>   I don't think you can do recursion in Euphoria without the routine_id
>
>   function or does your code work different ?

As per my code in the previous post, it works fine calling itself, so it
doesn't seem that it needs routine_id.

Nick

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

9. Re: converting sequence to string

Bernie Ryan wrote:

> On Sun, 26 Mar 2000 17:58:43 -0500, Irv Mullins <irv at ELLIJAY.COM> wrote:
>
> >You can do recursion without routine_id, because the function is declared
> before
> >the line that calls it.
>
>   Irv:
>
> This is the example I was replying to:
> --
> global function deNest(sequence nestedSequence)
> --deNests a sequence by recursing through it and 'flattening' it
> to 1 level
> sequence outputSequence outputSequence = {}
> for a=1 to length(nestedSequence) do
>   if not sequence(nestedSequence[a]) then
>     outputSequence = outputSequence&nestedSequence[a]
>   else outputSequence = outputSequence &
>     deNest(nestedSequence[a])    -- <<<<<<<<<<<<<<<<< --- look here
>   end if
> end for
> return outputSequence
> end function
>
>   If I run this in Euphoria how will Euphoria know what deNest function
>   looks like the first time in the first pass of the interpeter without
>   seeing the whole function first thats why I thought that a routine_id
>   function, a call_func function and routine_id variable is necessary.
>   This needs a forward reference to resolve this code so a Euphoria does
>   not error out on the first pass.
>   Maybe I'am wrong so straight my thinking out ?
>   Maybe you have a different trick to do this ?
>
>   Bernie

Weeeell, I tested the code here with a deeply nested sequence and it worked
fine, no problems. My understanding is that because this function is being
called from some code further down (what starts the whole thing), the
function has already been read and tokenised into memory, so when you call it
from the other code, the whole thing is already there and it can call it'self
to hearts desire :)

Nick

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

10. Re: converting sequence to string

On Sun, 26 Mar 2000 20:16:31 -0500, Irv Mullins <irv at ELLIJAY.COM> wrote:

>The reason it can work is because the function is declared before it is
called.


  I stand corrected.

  I have been writing some code in one of my libraries that I was
  writing and was using a case of recursion and kept getting errors
  from Euphoria saying that the function had not been declared.
  I added a routine_id to resolve the problem and it worked so I went
  on coding. Well I just now went to the function and remove the routine_id
  and it now works. Now it may have been some code that I have changed
  at a later time or You worked some magic to fix it.

  Thanks
  Bernie

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

11. Re: converting sequence to string

Irv Mullins wrote:

>The reason it can work is because the function is declared before it is called.
>It doesn't matter what code is inside the function...end function block,
>Euphoria doesn't need to know that, just the fact that there _is_ a function
>named deNest (and where it's located, I guess)
>
>By the way, this is not a forward reference. The call to
>deNest(nestedSequence(a)) is _after_ the declaration of the routine
>by the same name.
>
>As far as I know, code like the above will work even in languages which don't
>support forward references.
>
>Regards,
>Irv

Which translates to me into saying that the function header is sufficient
to provide the reference for the function. The entire function block is not
required.

Everett L.(Rett) Williams
rett at gvtc.com

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

12. Re: converting sequence to string

Here's a recursive routine that will "flatten out" a
deeply nested sequences of atoms and sequences.

global function flatten_seq( sequence s )
  atom ec  sequence ns
  ns = {}  ec = 0
  for i = 1 to length(s) do
    if atom(s[i]) then
      ns &= s[i]
      ec += 1
    else
      ns &= flatten_seq(s[i])
    end if
  end for
  if ec = length(s) then return {ns}
  else return ns
  end if
end function

Instead of simply taking a sequence and making it have all
atoms inside, this will preserve "strings". For example:

Input: {1,2,3,{4,5,{"hi",{"guy","girl"},6,'7'}},8,9,10}
Output: {1,2,3,4,5,"hi","guy","girl",6,'7',8,9,10}

The only downside is if the sequence has only atoms in it
to begin with, then it will make them into a single string :)

----->Buddy
budmeister1 at juno.com
http://tenbux.iwarp.com/

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

Search



Quick Links

User menu

Not signed in.

Misc Menu