1. converting sequence to string
- Posted by =?iso-8859-2?B?qWtvZGE=?= <tone.skoda at SIOL.NET> Mar 26, 2000
- 726 views
- Last edited Mar 27, 2000
------=_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 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 such = function but it=20 would be quite complicated because there can be unlimited number of = sequences=20 ------=_NextPart_000_0005_01BF9760.33762AA0--
2. Re: converting sequence to string
- Posted by Nick Johnson <arachnid at MAD.SCIENTIST.COM> Mar 26, 2000
- 635 views
- Last edited Mar 27, 2000
©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
3. Re: converting sequence to string
- Posted by Bernie Ryan <xotron at BUFFNET.NET> Mar 26, 2000
- 599 views
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 ?
4. Re: converting sequence to string
- Posted by Kat <gertie at ZEBRA.NET> Mar 26, 2000
- 583 views
----- 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
5. Re: converting sequence to string
- Posted by Irv Mullins <irv at ELLIJAY.COM> Mar 26, 2000
- 572 views
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
6. Re: converting sequence to string
- Posted by Bernie Ryan <xotron at BUFFNET.NET> Mar 26, 2000
- 557 views
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
7. Re: converting sequence to string
- Posted by Irv Mullins <irv at ELLIJAY.COM> Mar 26, 2000
- 558 views
- Last edited Mar 27, 2000
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
8. Re: converting sequence to string
- Posted by Nick Johnson <arachnid at MAD.SCIENTIST.COM> Mar 27, 2000
- 563 views
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
9. Re: converting sequence to string
- Posted by Nick Johnson <arachnid at MAD.SCIENTIST.COM> Mar 27, 2000
- 547 views
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
10. Re: converting sequence to string
- Posted by Bernie Ryan <xotron at BUFFNET.NET> Mar 26, 2000
- 568 views
- Last edited Mar 27, 2000
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
11. Re: converting sequence to string
- Posted by Everett Williams <rett at GVTC.COM> Mar 26, 2000
- 588 views
- Last edited Mar 27, 2000
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
12. Re: converting sequence to string
- Posted by budmeister1 at JUNO.COM Mar 27, 2000
- 602 views
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/