| View previous topic :: View next topic |
| Author |
Message |
dmonckton Contributor


Joined: 09 Aug 2002 Posts: 117 Location: Lewes, U.K.
|
Posted: Thu Mar 27, 2008 11:43 am Post subject: Recursion? |
|
|
Hi
Can VDS have recusive functions? I'm trying to write a merge sort function but it has to call itself.
| Code: |
#define command,mergeSort
Title mergeSort
DIALOG CREATE,New Dialog,-1,0,382,504
DIALOG ADD,LIST,L1,21,33,293,340
DIALOG ADD,BUTTON,GO,387,166,64,24,GO
DIALOG ADD,STATUS,ST1
DIALOG SHOW
list loadtext,L1
"Electric Grand Piano
"Honky-tonk Piano
"Harpsichord
"Clavi
"Celesta
"Glockenspiel
"Music Box
"Vibraphone
"Marimba
"Xylophone
"Bright Acoustic Piano
"Tubular Bells
"Acoustic Grand Piano
"Dulcimer
"Electric Piano 1
"Electric Piano 2
:Evloop
wait event
goto @event()
:GOBUTTON
mergeSort L1,0,@lindex(L1)
goto evloop
:mergeSort
if @lesser(%2,%3)
%m = @div(@sum(%2,%3),2)
mergeSort L1,%2,%m
mergeSort L1,@succ(%m),%3
end
exit
|
This does'nt work, VDS has a problem with mergeSort appearing inside mergeSort!!
Any ideas??
Thanks
David.M |
|
| Back to top |
|
 |
Aslan Valued Contributor


Joined: 31 May 2001 Posts: 589 Location: Memphis, TN USA
|
Posted: Fri Mar 28, 2008 2:49 am Post subject: |
|
|
Hey David, long time no see
Your script shows "mergeSort L1,0,@lindex(L1)"
@lindex() isn't a function
did you mean "mergeSort L1,0,@index(L1)"
Also your mergeSort command will put you into an infinite loop because it can never finish.
Maybe you could create two commands that do the same thing that can use each other. Problem here is that in your example %2 and %3 would never change which would also put you in an infinite loop. Of course, this concept would be the same as using 'While / Wend'
Exactly what is it you want mergeSort to do?  |
|
| Back to top |
|
 |
vdsalchemist Admin Team

Joined: 23 Oct 2001 Posts: 1448 Location: Florida, USA
|
Posted: Fri Mar 28, 2008 3:44 pm Post subject: |
|
|
David,
I think the limit for recursive functions is 16 or 32? It depends on which version of VDS your using. I don't know why VDS has a limit like that but I suspect it has something to do with the fact that it is single threaded. Jules is probably the only person that could answer this with any detail. _________________ Home of
Give VDS a new purpose!
 |
|
| Back to top |
|
 |
vdsalchemist Admin Team

Joined: 23 Oct 2001 Posts: 1448 Location: Florida, USA
|
Posted: Fri Mar 28, 2008 3:47 pm Post subject: |
|
|
According to the VDS 6 help file the limit is 16...
| Quote: | | GOSUB commands may be nested a maximum of 16 levels deep. |
A VDS user defined function is technically the same as a GOSUB. _________________ Home of
Give VDS a new purpose!
 |
|
| Back to top |
|
 |
dmonckton Contributor


Joined: 09 Aug 2002 Posts: 117 Location: Lewes, U.K.
|
Posted: Wed Apr 02, 2008 8:39 am Post subject: |
|
|
Hi
Sorry for the slow reply I've had a really bad cold and been laid up!
Anyway @lesser() and @lindex() are some library functions I wrote to be lazy.
| Code: |
:lesser
if @not(%1)@not(%2)
warn Syntax error in "@lesser()"
stop
end
if @greater(%1,%2)
exit
elsif @equal(%1,%2)
exit
else
exit 1
end
end
exit
:lindex
if @not(%1)
warn Syntax error in "@lindex()"
stop
end
%a = @pred(@count(%1))
exit %a
|
So VDS functions cannot call themselves? I'll have to us goto's to control the branching? What happens with the local variables, does a new set get created only if I us GOSUB rather than GOTO?
I'm trying to implement the Merge Sort Pseudocode found at http://en.wikipedia.org/wiki/Merge_sort but this requires a lot of recursion.
Thanks
David... |
|
| Back to top |
|
 |
jules Professional Member


Joined: 14 Sep 2001 Posts: 1043 Location: Cumbria, UK
|
Posted: Thu Apr 03, 2008 8:20 am Post subject: |
|
|
Yes, VDS functions can recurse. There is a limit to the level of nesting, as there is with gosubs etc. I can't remember what the limit is, though. (You could write a recursive function to find it out.) Only the %x variables are local to each call, the %%xx variables are global to the entire program. HTH. _________________ The Tech Pro
www.tech-pro.net |
|
| Back to top |
|
 |
vdsalchemist Admin Team

Joined: 23 Oct 2001 Posts: 1448 Location: Florida, USA
|
Posted: Thu Apr 03, 2008 4:19 pm Post subject: |
|
|
Below is some code to show the number of times a sub-routine can call itself in VDS.
| Code: |
# Demostrate VDS recurssion...
# The number of times a function (ie... sub-routine) can call itself...
# The value for VDS 6 is 16... IMHO this is way too low to be of any real usage.
#DEFINE FUNCTION,RECURSE
%%Count = 0
%A = @Recurse()
:RECURSE
%%Count = @succ(%%Count)
Info Recursive Count = %%Count
%A = @Recurse()
Exit
Info I never get here.
Stop
|
_________________ Home of
Give VDS a new purpose!
 |
|
| Back to top |
|
 |
dmonckton Contributor


Joined: 09 Aug 2002 Posts: 117 Location: Lewes, U.K.
|
Posted: Mon Apr 28, 2008 9:25 pm Post subject: |
|
|
Thanks for the info and the example, cleared things up for me.
David.M |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|