|
Post by bigbass on Aug 4, 2013 19:52:59 GMT 1
Here is a way to see recursion in action notice there is movement without loops being used ! any combination of swapping could be made into an easy formula lets say you want to switch the first and the third string in an array short hand notation 0-->3 2-->0 3-->2 this means 0 moves to 3 2 moves to 0 3 moves to 2 the array notation is a bit ugly but the short hand idea is the same '--- swap the first and the third string DECLARE A$[3] TYPE STRING
A$[0]="one" A$[1]="two" A$[2]="three"
A$[3]=A$[0] A$[0]=A$[2] A$[2]=A$[3]
PRINT A$[0] PRINT A$[1] PRINT A$[2]
'--- swap or reverse four strings from left to right '--- so that the array now holds four three two one
DECLARE A$[4] TYPE STRING
A$[0]="one" A$[1]="two" A$[2]="three" A$[3]="four"
A$[4]=A$[0] A$[0]=A$[3] A$[3]=A$[1] A$[1]=A$[2] A$[2]=A$[3] A$[3]=A$[4]
PRINT A$[0] PRINT A$[1] PRINT A$[2] PRINT A$[3]
ok had to do this on paper first using coins of different sizes to get eight strings to reverse thats enough to show some examples for recursive programming short hand notation of the movement of the full strings 0-->8 7-->0 1-->7 6-->1 2-->6 5-->2 3-->5 4-->3 5-->4 6-->5 7-->6 8-->7 '--- swap or reverse eight strings from left to right '--- so that the array now holds eight strings from high to low now
DECLARE A$[7] TYPE STRING
A$[0]="one" A$[1]="two" A$[2]="three" A$[3]="four" A$[4]="five" A$[5]="six" A$[6]="seven" A$[7]="eight"
A$[8]=A$[0] A$[0]=A$[7] A$[7]=A$[1] A$[1]=A$[6] A$[6]=A$[2] A$[2]=A$[5] A$[5]=A$[3] A$[3]=A$[4] A$[4]=A$[5] A$[5]=A$[6] A$[6]=A$[7] A$[7]=A$[8]
PRINT A$[0] PRINT A$[1] PRINT A$[2] PRINT A$[3] PRINT A$[4] PRINT A$[5] PRINT A$[6] PRINT A$[7]
|
|
|
Post by bigbass on Dec 21, 2018 23:53:08 GMT 1
added a simple macro to express the idea of an array copy to show how little code is needed
'--- this is a recursive approach to moving strings
'--- copy s2 array to s1 array DEF FN ARRAY_CP(s1,s2) = ( (s1=s2) )
DECLARE A$[4] TYPE STRING A$[0] = "xxforxx" A$[1] = "xx" A$[2] = "Geeks" A$[3] = "for"
'--- this overwrites xxforxx in [0] with Geeks ARRAY_CP(A$[0], A$[2]) PRINT A$[0]
'--This overwrites Geeks with for ARRAY_CP(A$[0], A$[3]) PRINT A$[0]
'--- This overwrites for with Geeks ARRAY_CP(A$[0], A$[2]) PRINT A$[0]
|
|
|
Post by bigbass on Dec 22, 2018 17:43:49 GMT 1
the easy way to do dynamic array index with strings
DECLARE A$[7] TYPE STRING
A$[0]="one" A$[1]="two" A$[2]="three" A$[3]="four" A$[4]="five" A$[5]="six" A$[6]="seven" A$[7]="eight" '---dynamic index WHILE (A$[i] ISNOT NULL) PRINT A$[i] INCR i WEND
|
|
|
Post by bigbass on Mar 22, 2020 3:09:55 GMT 1
Hello guys
This thread got no feedback and its actually one of my favorites because I just sit down with a pencil and paper or in the first two examples I used just coins of different sizes and wrote out all the code like a homemade simple game ( I guess I was bored) then wrote the code
this example seems easy on the surface but its more complex when you try to code it into a concat array function'
'--- we will "concat" two arrays and make a new array for the result '--- all done recursively non looping non movement '--- recursion ( I did this on paper first without any code)
DEF FN A_CAT(s1,s2,s3,s7,s8,s9) = ( (s7=s1) ,(s8=s2) ,(s9=s3) ) DEF FN B_CAT(s1,s2,s3,s10,s11,s12) = ( (s10=s1), (s11=s2) ,(s12=s3 ) )
DECLARE A$[3] TYPE STRING A$[0]="one" A$[1]="two" A$[2]="three"
DECLARE B$[3] TYPE STRING B$[0]="four" B$[1]="five" B$[2]="six"
DECLARE C$[6] TYPE STRING C$[0] = "" C$[1] = "" C$[2] = "" C$[3] = "" C$[4] = "" C$[5] = ""
A_CAT(A$[0],A$[1],A$[2],C$[0],C$[1],C$[2]) B_CAT(B$[0],B$[1],B$[2],C$[3],C$[4],C$[5])
PRINT C$[0] PRINT C$[1] PRINT C$[2] PRINT C$[3] PRINT C$[4] PRINT C$[5]
TERMINAL OUT one two three four five six
|
|
|
Post by Pjot on Mar 22, 2020 18:44:19 GMT 1
Thanks Joe,
Your code is correct and will work.
So just to be nerdy, how about the following: DECLARE a$[] = { "one", "two", "three" } DECLARE b$[] = { "four", "five", "six" }
DECLARE c$[6] TYPE STRING
OPTION DELIM NL$
PRINT COIL$( 3, c$[_-1] = a$[_-1] ) PRINT COIL$( 3, c$[_+2] = b$[_-1] )
BR Peter
|
|
|
Post by bigbass on Mar 22, 2020 19:14:25 GMT 1
Hello Peter Thanks I like it! ( and very compact and to the point) And I really see a lot of power with this new COIL$ command I was testing it before with ascii and it is impressive for the little I thought to do with it there are three things I leaned with your example 1.)no need to give a value inside the [] I guess the compiler is smart enough to figure that out 2.)using the option OPTION DELIM NL$ with COIL$ 3.) using the _ inside the [] I didn't know that was possible and hoped for some type of range function for that but didn't know how to make it work thanks again an excellent demo using COIL$ yes a bit nerdy but we do away with FOR/NEXT WHILE /WEND and some counters INCR I am unfamiliar with all the potential with COIL$ so it's great that you posted some of your ideas how to use it Joe
|
|
|
Post by bigbass on Mar 23, 2020 2:44:10 GMT 1
Hello Peter
Those COIL$ and LOOP$ commands are not only cool they are also very fast and a nice surprise we can do multiple commands separated by a comma
It actually gives the result in a new array C$[]
DECLARE A$[] = {"one", "two", "three"} TYPE STRING
DECLARE B$[] = {"four", "five", "six"} TYPE STRING
DECLARE C$[6] TYPE STRING
PRINT COIL$(i, 6, C$[i-1]= A$[i-1] ,C$[i+2]= B$[i-1])
PRINT COIL$(i, 6, C$[i-1]= A$[i-1] ,C$[i+3]= B$[i+3]) the B$ offset printed correctly but the c$ array was off now fixed above code corrected
and if we use LOOP$ instead of COIL$ they are really concatted
P.S A lot of macros could be rewritten to take advantage of doing the coding on the BaCon code side of the memory with COIL$ and LOOP$
Joe
|
|
|
Post by Pjot on Mar 24, 2020 20:56:32 GMT 1
Hi Joe, Well, that's an interesting suggestion indeed - using multiple assignments, separated by a comma, within one COIL$ function. Never would have thought of this myself... However, your code does not seem to be working fully for me, but with a small modification the following actually works: DECLARE a$[] = { "one", "two", "three" } DECLARE b$[] = { "four", "five", "six" }
DECLARE c$[6] TYPE STRING
OPTION DELIM NL$
PRINT COIL$(i, 3, ( c$[i-1] = a$[i-1], c$[i+2] = b$[i-1]) )
The additional brackets will make sure the parser takes the whole argument into account. The printed values only are the last assignments, but the whole c$[] array will in fact be assigned the correct strings. I have improved the LOOP/LOOP$/COIL$ code further (latest in fossil), now the anonymous variable also works: PRINT COIL$(3, ( c$[_-1] = a$[_-1], c$[_+2] = b$[_-1]) )
Even shorter! Thanks for this idea, Best regards,
Peter
|
|
|
Post by bigbass on Mar 25, 2020 7:22:35 GMT 1
hello Peter yes it printed correctly for me but my offset was off (sorry) I used strike out and left my old error there as a reference so I wont forget zero index array but i starts at 1 I know we only need three loops to do the array correctly I used 6 because it cheats and saves in all in one PRINT command your code is correct in that it assigns the array with 3 loops thanks all of this is fun and where I live in Mexico there is so much chaos its good to think about something else like coding for awhile here is some debugging code I used to test with
'--- UNDOCUMENTED STRING EXAMPLE Initialize 2D array DECLARE A$[] = {"one", "two", "three"} TYPE STRING DECLARE B$[] = {"four", "five", "six"} TYPE STRING
DECLARE C$[6] TYPE STRING
'prints 123456 PRINT COIL$(i, 6, C$[i-1]= A$[i-1] ,C$[i+2]= B$[i-1])
'debugging code 'this prints 456123 'PRINT COIL$(6, ( C$[_-1] = A$[_-1], C$[_+2] = B$[_-1]) )
i=0 WHILE i < 6 PRINT C$[i] INCR i WEND
|
|
|
Post by Pjot on Mar 25, 2020 18:48:02 GMT 1
Hi bigbass, Your construction is known as the " Comma Operator" in C. There are some nice examples on that Wiki page.
We can use this functionality in LOOP$ and friends, which is very convenient.
BR Peter
|
|
|
Post by bigbass on Mar 26, 2020 20:21:11 GMT 1
Hello Peter
the wiki link very informative with clear examples the best part of this is, bacon can parse those special options when needed thanks
Joe
|
|
|
Post by Pjot on Mar 27, 2020 21:46:24 GMT 1
Hi Joe,
Just to let you know that COIL$ now also allows an optional 4th argument to specify the delimiter (latest in fossil):
PRINT COIL$(i, 3, ( c$[i-1] = a$[i-1], c$[i+2] = b$[i-1]) , NL$)
Note that this only works in case COIL$ has 4 arguments.
The reason is that the parser cannot distinguish between 3 COIL$ arguments where the first argument defines a variable name, or 3 arguments using an anonymous variable and where the last argument defines a delimiter. So only in case of 4 arguments the parser will know the last argument must be a delimiter.
Best regards Peter
|
|