Challenge: Recursive Recluse

• (2 Pages)
• 1
• 2

22 Replies - 15049 Views - Last Post: 16 September 2013 - 09:09 AM

#16 Witchking

Reputation: 68
• Posts: 189
• Joined: 17-February 13

Re: Challenge: Recursive Recluse

Posted 13 September 2013 - 05:40 AM

Technically it's a constant, not a variable . I could have declared it at the start of the function, but it seemed wasteful to declare it again for each recursive call. Having it as another argument though, is fine as well i suppose.

I guess i should have a explained a bit about how it works. See the spoiler.

Spoiler

This post has been edited by Witchking: 13 September 2013 - 05:45 AM

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• Joined: 29-May 08

Re: Challenge: Recursive Recluse

Posted 13 September 2013 - 05:44 AM

My Implementation of the Matching Brace Position Challenge.

Spoiler

It maybe a constant but you reference it via a named variable Brackets which is external and not a parameter that is passed in. (As per the challenge)

This post has been edited by AdamSpeight2008: 13 September 2013 - 05:48 AM

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• Joined: 29-May 08

Re: Challenge: Recursive Recluse

Posted 13 September 2013 - 06:06 AM

Is slightly tweaked the lambda.
Spoiler

• Home Owner

Reputation: 571
• Posts: 2,647
• Joined: 11-May 12

Re: Challenge: Recursive Recluse

Posted 13 September 2013 - 06:39 AM

You failed your own challenge

Quote

So your challenge is to slay that beast by not only writing a recursive function, but also writing an informative explanation for a learner to understand it.

I'm a learner with no former knowledge of what a lambda expression is

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• Joined: 29-May 08

Re: Challenge: Recursive Recluse

Posted 13 September 2013 - 02:00 PM

A variation of Witchking's method
Spoiler

#21 andrewsw

• never lube your breaks

Reputation: 6829
• Posts: 28,317
• Joined: 12-December 12

Re: Challenge: Recursive Recluse

Posted 13 September 2013 - 02:08 PM

I'm with ybadragon. @Adam to meet your own challenge you have to provide an informative explanation

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• Joined: 29-May 08

Re: Challenge: Recursive Recluse

Posted 13 September 2013 - 02:33 PM

It doing the exact same thing as Witchking's method. So why duplicate the explanation?
Only difference is how it calculate the next step.

```Dim i = "} {".IndexOf(Code(Pos)) - 1
' Code(Pos) =
'             "}"  => i = -1
'             "{"  => i = +1

```

Calculating the index of the opposite brace, is handled by the following.
```"] ["(-1 * i + 1)

-1 * -1 ("}") =>  1 + 1 => 2 Index of matching brace
-1 *  1 ("{") => -1 + 1 => 0 Index of matching brace

```

On subject of lambda expressions, is essentially using function as value (something that can be passed around and used.

If you look a the early example you'll see I'm embedding another function inside a function.

This post has been edited by AdamSpeight2008: 13 September 2013 - 02:36 PM

#23 happyvisu

• New D.I.C Head

Reputation: 1
• Posts: 21
• Joined: 03-August 13

Re: Challenge: Recursive Recluse

Posted 16 September 2013 - 09:09 AM

Example: like scan a folder
```Sub scanfolder(byval currdir as String)
if interrupt Then Exit Sub
Dim Dir As String
Dim File As String
Dim FI As FileInfo
Label1.Text = "Scanning " & currdir
For Each File In Directory.GetFiles(currdir)
FI = New FileInfo(File)
FileList.Items.Add(FI.Name & vbTab & FI.Length & vbTab & FI.CreationTime)
Next
countFiles += Directory.GetFiles(currdir).Length
Me.Text = "Scanning " & FolderList.Text & " "-Processed " & countFiles & "files in " & countFolders & "folders..."
For Each Dir In Directory.GetDirectories(currdir)
countFolders +=1
Application.DoEvents()
ScanFolder(Dir)
Next
End Sub

```

Recursion function must exit explicitly
Examine the code:
```Function DoSomething(n as Integer) As Integer
{other statements}
value=value-1
if value = 0 then Exit Function
newvalue = DoSomething(value)
{more statements}
End Function

Example: like scan a folder
Sub scanfolder(byval currdir as String)
if interrupt Then Exit Sub
Dim Dir As String
Dim File As String
Dim FI As FileInfo
Label1.Text = "Scanning " & currdir
For Each File In Directory.GetFiles(currdir)
FI = New FileInfo(File)
FileList.Items.Add(FI.Name & vbTab & FI.Length & vbTab & FI.CreationTime)
Next
countFiles += Directory.GetFiles(currdir).Length
Me.Text = "Scanning " & FolderList.Text & " "-Processed " & countFiles & "files in " & countFolders & "folders..."
For Each Dir In Directory.GetDirectories(currdir)
countFolders +=1
Application.DoEvents()
ScanFolder(Dir)
Next
End Sub

```

This post has been edited by andrewsw: 16 September 2013 - 09:13 AM
Reason for edit:: Fixed code tags