Function Libraries and Recursion

PowerShell Functions mean never having to repeat yourself

Because you can reuse them in lot’s of scripts – my “write-log” function which I’ll upload soon is one such example.  I usually store my functions in separate scripts in a scripts library folder on my PC ready for reuse on my next project.

The best practice that most people use is to create a library script file of functions that you can preload into all of your scripts using dot sourcing.   This is actually a good idea but there is one problem with this approach that over time I tend to modify my function and improve on them.  If I use a single library file then I’d need to check that I didn’t just break the rest of my scripts that call the same functions.  It’s certainly good practice to use a single library function to do this, but for me, I just like to cut and paste in the functions I need for any new scripts so I can test as part of writing the new script, enhance it if I come across something I hadn’t thought about when I originally wrote it and then save it back into the original file in my source library folder.

I keep each one separate and call the file by the name of the function, which should hopefully tell me what the function actually does.  I stick to using the approved verbs / nouns used for PowerShell cmdlets – see here for a list.

Another strategy is to convert all of your functions into a PowerShell module and load this either into your PowerShell profile or use Get-Module to load it into your script.  I do this for my Excel Spreadsheet functions to simplify updating excel spreadsheets – this is work in progress but I do plan to upload it into my blog soon if anyone is interested.

Recursion is a wonderful thing that makes difficult tasks easy!

Recursion is a function that behaves like the Ouzelum bird.  Well actually you want to make sure it’s can’t disappear completely so yo uneed to make sure,  in a similar way you would do in any programming loop, that there is an exit condition otherwise you will run out of resource and your program will never end.

I remember being taught about recursion when studying programming years ago and the example used to explain it was a program to solve the Towers of Hannoy problem.   It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.  The objective of the puzzle is to move the entire stack to another rod, obeying the following rules:

  1. Only one disk must be moved at a time.
  2. Each move consists of taking the upper disk from one of the rods and sliding it onto another rod, on top of the other disks that may already be present on that rod.
  3. No disk may be placed on top of a smaller disk.

With three disks, the puzzle can be solved in seven moves.

Now how do you write a script to solve that?  As a human you might do it in seven moves?  Is that true?  Well actually no because before you make any actual moves you will be thinking about the possible moves and where those moves might lead you.

When learning about programming this is often the example used to teach you how powerful recursion is as a programming tool – you can find a ready made PowerShell version of this here.

The cool thing about using recursion (ohhh that’s a bit sad that I think recursion is cool, sorry but I’ll continue anyway) is that it breaks a problem down into manageable chunks.  The Towers of Hannoy is not a difficult puzzle but it’s great at demonstrating the power of recursion.  So how do we solve this problem?  If we only had to move one disk to solve the problem then writing a function to do this would be easy right?  We can write a recursive function to move the last disk to the correct rod.  Easy but what about when there are more disks on top.  Simples we call the same function to move all the rest of the disks to the middle rod.  If we keep doing this eventually we end up with a single disk on rod 1 and then we can just move it to rod 3.

Using a slightly more formal explanation using psudo code….

If we have N disks then to move all the disks to the last rod you need to move the bottom most disk to the last rod.  However, to do so, you need to move the N-1 disks above the bottom most disk to some other rod first. Moving the N-1 disks away is equivalent to solving the Tower of Hanoi Problem with N-1 disks.  here is this in some psudo code:

Function SolveTowerOfHanoi (N Disks) {
SolveTowerOfHanoi(N-1 Disks)  to move them away from the bottom disk
Move the bottom-most disk over to the target rod
SolveTowerOfHanoi(N-1 Disks) to move the disks we moved in step 1 on top of the bottom-most disk we just moved.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.