<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blog 4 &#187; What In The Hell Series</title>
	<atom:link href="http://nickzarr.com/blog4/category/programming-2/what-in-the-hell-series/feed/" rel="self" type="application/rss+xml" />
	<link>http://nickzarr.com/blog4</link>
	<description>Chosen by fair dice roll. Guaranteed random.</description>
	<lastBuildDate>Sun, 22 Apr 2012 18:29:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>What In The Hell Are Race Conditions and Locks</title>
		<link>http://nickzarr.com/blog4/2011/03/what-in-the-hell-are-race-conditions-and-locks/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-in-the-hell-are-race-conditions-and-locks</link>
		<comments>http://nickzarr.com/blog4/2011/03/what-in-the-hell-are-race-conditions-and-locks/#comments</comments>
		<pubDate>Wed, 16 Mar 2011 04:09:03 +0000</pubDate>
		<dc:creator>Nick Zarczynski</dc:creator>
				<category><![CDATA[What In The Hell Series]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[concurrent]]></category>
		<category><![CDATA[Learn To Program]]></category>
		<category><![CDATA[locks]]></category>
		<category><![CDATA[parallel]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[race condition]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://nickzarr.com/blog4/?p=853</guid>
		<description><![CDATA[In the context of parallel or concurrent programs, you will often hear the term race condition. This describes a problem when two pieces of code try to modify the same variable (or other location in memory). Race conditions can be notoriously difficult to find, reproduce and debug. To start with we have a variable a [...]]]></description>
			<content:encoded><![CDATA[<p>In the context of parallel or concurrent programs, you will often hear the term <strong>race condition</strong>.  This describes a problem when two pieces of code try to modify the same variable (or other location in memory).</p>
<p>Race conditions can be notoriously difficult to find, reproduce and debug.</p>
<p>To start with we have a variable <strong>a</strong> that is set to <strong>7</strong>.  I&#8217;ll show you what can happen, step-by-step, when two pieces of code try to use and modify that variable.</p>
<p><img src="https://docs.google.com/drawings/pub?id=1iinn0_zqINILQILp6V7YMNEXIK9j6iUwaNDvEjlQqrY&amp;w=621&amp;h=543"></p>
<p>Both <strong>a = a + 35</strong> and <strong>a = a &#8211; 10</strong> will execute &#8220;at the same time&#8221;.  I have &#8220;at the same time&#8221; in scare quotes because we don&#8217;t really know what &#8220;at the same time&#8221; means.  Computers process instructions sequentially, this means that one operation has to come before the other.  Let&#8217;s see how that can play out&#8230;</p>
<p><img src="https://docs.google.com/drawings/pub?id=1x17OV3fwC2G8-NyvGPetnJPmLyqRSzn4fQIcnZYaoE4&amp;w=621&amp;h=543"></p>
<p><img src="https://docs.google.com/drawings/pub?id=140M4fCGA_tkKk2k9G9eHpjNythQm6cFMM_Z9vGdNDaY&amp;w=620&amp;h=567"></p>
<p><img src="https://docs.google.com/drawings/pub?id=1spIroi1ms8mFGqAPSu0Y8iPueueuDV3iWNuZYAWGQRY&amp;w=620&amp;h=567"></p>
<p><img src="https://docs.google.com/drawings/pub?id=1rf89OYew7xqBh_yjxjlaquF2FDFtPAmqdkZ76i59AxY&amp;w=620&amp;h=567"></p>
<p>From looking at the code in the beginning, you may have guessed that the value of <strong>a</strong> after these operations would be <strong>42</strong>, and that was indeed the intent.  However, because of the <strong>race condition</strong> the value of <strong>a</strong> ended up being <strong>-3</strong>.  Not at all what we wanted!</p>
<p>Probably the most common way to handle race conditions is to use <strong>locks</strong>.  Here&#8217;s how the above process would look using locks&#8230;</p>
<p><img src="https://docs.google.com/drawings/pub?id=1-xZukfFhGKSOVYFInmfhDOZg1QjrJhc7DM9INN8uiHg&amp;w=621&amp;h=543"></p>
<p><img src="https://docs.google.com/drawings/pub?id=1LrHIN-TybwsD-1fNd_H5_9cwIcDpUTvRZQYRzENpQ60&amp;w=621&amp;h=543"></p>
<p><img src="https://docs.google.com/drawings/pub?id=1AqGVtqT0dLbg8LBdPslZRBndO3UeRmmC7y0fyWk0lm8&amp;w=620&amp;h=567"></p>
<p><img src="https://docs.google.com/drawings/pub?id=1x5fXzPxOYM9LtgMH7-jjwtssXWXb983LdxD_y3bN7-w&amp;w=620&amp;h=567"></p>
<p><img src="https://docs.google.com/drawings/pub?id=1BQYtqkSFQwi5vU2bqM-eLxqLCXOtcyzqarOUqp7VHbw&amp;w=620&amp;h=567"></p>
<p><img src="https://docs.google.com/drawings/pub?id=1Pe-Rq_lhI8p1maafl7BqHvWhTTPsaz1ynb8P7zSYZd8&amp;w=620&amp;h=567"></p>
<p><img src="https://docs.google.com/drawings/pub?id=1mBf5cdvhHkWTfZKCgf55fTA6quZa6rgtvZp0YLyEWZQ&amp;w=620&amp;h=567"></p>
<p>Locks prevent race conditions by making sure that only one piece of code can access a memory location at any time.  Locks can become very complicated to manage and are themselves the source of many bugs.  Consider what would happen if some piece of code did not unlock the memory it was modifying!  Or worse, did not lock it in the first place, in which case we fall back to the race condition.</p>
<p>Locks are not the only way to handle race conditions (though they are the most common).  Another way to handle race conditions is to use immutable variables.  This means that no code is allowed to change the value of <strong>a</strong>.  Immutable variables will be the subject of another WITH article, so I won&#8217;t explain them here.</p>
<p><a href="http://nickzarr.com/blog4/what-in-the-hell-series/">GOTO Table of Contents</a></p>
]]></content:encoded>
			<wfw:commentRss>http://nickzarr.com/blog4/2011/03/what-in-the-hell-are-race-conditions-and-locks/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>What In The Hell &#124; Basic Numeric Data Types (Char, Integer, Short, Signed)</title>
		<link>http://nickzarr.com/blog4/2011/03/what-in-the-hell-basic-numeric-data-types-char-integer-short-signed/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-in-the-hell-basic-numeric-data-types-char-integer-short-signed</link>
		<comments>http://nickzarr.com/blog4/2011/03/what-in-the-hell-basic-numeric-data-types-char-integer-short-signed/#comments</comments>
		<pubDate>Sat, 05 Mar 2011 01:54:01 +0000</pubDate>
		<dc:creator>Nick Zarczynski</dc:creator>
				<category><![CDATA[What In The Hell Series]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[data types]]></category>
		<category><![CDATA[integer]]></category>
		<category><![CDATA[learn programming]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://nickzarr.com/blog4/?p=539</guid>
		<description><![CDATA[Numbers are an abstract idea. A concept that can only be approximated in reality. To illustrate this concept a little better, take out some paper and write down the answer to 1/3. Are you done? How could you be? Whatever you wrote down I could make a better by adding another 3 at the end, [...]]]></description>
			<content:encoded><![CDATA[<p>Numbers are an abstract idea.  A concept that can only be approximated in reality.  To illustrate this concept a little better, take out some paper and write down the answer to 1/3.</p>
<p>Are you done?  How could you be?  Whatever you wrote down I could make a better by adding another 3 at the end, or another trillion 3&#8242;s at the end.  How about the biggest number you can think of?  Any number you can write down I can make better by adding one more digit.</p>
<p>So you can see that we have to put some limits somewhere.  The limits that we put on numbers in a computer correspond to the amount of memory it takes to represent those numbers.</p>
<p>To understand this, we need to know a little bit about computer memory.  Computers store information in <strong>bits</strong>.  A <strong>bit</strong> is a binary number (1 or 0) that represents the on or off state of the memory.  Since a single bit is often too little to use effectively, computer memory is organized into blocks of 8 bits called <strong>bytes</strong>.</p>
<p><img src="https://docs.google.com/drawings/pub?id=1I0_I1SA2l-Fw4RJEzxAsgxQTyvz4Zemw30G4UBdI3ig&amp;w=581&amp;h=100"></p>
<p>The 8 bit byte can represent the numbers 00000000 through 11111111 in binary, which is 0 to 255.  Your computer&#8217;s memory is organized into a big row of bytes.</p>
<p><img src="https://docs.google.com/drawings/pub?id=1mpEXyaq20b64-Z7uwp77ZycHmDZvFfwMAYiiQj4b1EM&amp;w=581&amp;h=241"></p>
<p>Each address above refers to one byte.  A <strong>char</strong> is one byte.  However, what do we do when we need a number bigger than 255?  We have to use more than one <strong>byte</strong>.  A number that is 2 bytes can represent a number from 0 to 65,535 and is called a <strong>short int</strong>.</p>
<p><img src="https://docs.google.com/drawings/pub?id=1lw5wO8y7bxpOv3d3Od-jNfOmH8HaTaKyr8HdRSvVoR0&amp;w=579&amp;h=121"></p>
<p>A 4 byte number (32 bit) could represent 0 to 4,294,967,295 and is called an <strong>int</strong> or <strong>integer</strong>.</p>
<p><img src="https://docs.google.com/drawings/pub?id=1KcZZqxd9a9gQ4SoP7hoX4YKMUUgZYQcq1LyadN_o8Q8&amp;w=579&amp;h=121"></p>
<p>An 8 byte number (64 bit) could represent 0 to 18,446,744,073,709,551,615 and is called a <strong>double</strong>.</p>
<p><img src="https://docs.google.com/drawings/pub?id=1Dv_JJcHf_VikmdqtvunsOGZcQPwls1fh4KC6UtZfPUI&amp;w=579&amp;h=121"></p>
<p>Another way to look at the basic numeric types is as names for sizes of memory.</p>
<h2>What happens when we want negative numbers?</h2>
<p>Let&#8217;s go back to the <strong>char</strong> or the single byte.  A char can represent 256 values and only 256 values, so to get negative numbers we need to give up some of the positive ones.  In fact we&#8217;ll give up exactly half of the positive ones and represent the numbers -128 through 127.</p>
<p>When we want to represent negative numbers we use a <strong>signed</strong> type.  <strong>Unsigned</strong> types represent only positive numbers and 0.  Any of char, short int, int or double can be signed or unsigned and each follows the split in half rule.</p>
<h2>What about the decimal point?</h2>
<p>Numbers with a decimal point in them are called <strong>floating point</strong> numbers.  An entire book can probably be written about floating point numbers as they can become quite complicated.  A future WITH article will cover floating point numbers in more detail.</p>
<p>* The size of each char, int, etc can vary depending upon the hardware.  This article used common sizes for a 32 bit architecture.<br />
* Teaching Tip:  Write down numbers on a whiteboard/paper and measure them with a ruler.  Imagine each inch is a block of memory.  How many different numbers can you store in one block?  How about two or four?  What would you name those blocks so you could talk about them with other people?  Bonus: how many blocks does it take to write a character?</p>
<p><a href="http://nickzarr.com/blog4/what-in-the-hell-series/">GOTO Table of Contents</a></p>
]]></content:encoded>
			<wfw:commentRss>http://nickzarr.com/blog4/2011/03/what-in-the-hell-basic-numeric-data-types-char-integer-short-signed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What In The Hell Is Recursion</title>
		<link>http://nickzarr.com/blog4/2011/03/what-in-the-hell-is-recursion/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-in-the-hell-is-recursion</link>
		<comments>http://nickzarr.com/blog4/2011/03/what-in-the-hell-is-recursion/#comments</comments>
		<pubDate>Fri, 04 Mar 2011 22:53:45 +0000</pubDate>
		<dc:creator>Nick Zarczynski</dc:creator>
				<category><![CDATA[What In The Hell Series]]></category>
		<category><![CDATA[compsci]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[learn programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[Scheme]]></category>
		<category><![CDATA[tail-call optimization]]></category>

		<guid isPermaLink="false">http://nickzarr.com/blog4/?p=548</guid>
		<description><![CDATA[GOTO Table of Contents &#160; Recursion is often a feared and dreaded word among new computer scientists. Most see it as a mystical process that they have no hope of understanding. Others loudly proclaim that they&#8217;ve been programming for years and have never needed recursion, mostly because they see it as a mystical process that [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://nickzarr.com/blog4/what-in-the-hell-series/">GOTO Table of Contents</a></p>
<p>&nbsp;</p>
<p>Recursion is often a feared and dreaded word among new computer scientists.  Most see it as a mystical process that they have no hope of understanding.  Others loudly proclaim that they&#8217;ve been programming for years and have never needed recursion, mostly because they see it as a mystical process that they have no hope of understanding.</p>
<p>However recursion is actually pretty straightforward.  And that&#8217;s a good thing, because there are a lot of problems that have very elegant recursive solutions.  We&#8217;ll cover some of those when we learn about <a href="http://nickzarr.com/blog4/2011/02/21/what-in-the-hell-are-trees/">tree</a> algorithms and sorting in future WITH articles.  </p>
<p>For now we&#8217;re going to focus on the basics of recursion.  Recursive functions are most often used as a form of loop and for this article we&#8217;ll use one of the most basic loops; counting.  We&#8217;re going to describe a process of printing all the numbers from 0 to infinity.  Without recursion you would probably write something similar to the following.</p>
<pre class="brush: python; title: ; notranslate">
def count(n):
  while 1:
    print n
    n = n + 1
</pre>
<pre class="brush: python; gutter: false; title: ; notranslate">
count(0)
0
1
2
...
</pre>
<p>Next we&#8217;ll look at the recursive solution.</p>
<pre class="brush: python; title: ; notranslate">
def count(n):
  print n
  count(n+1)
</pre>
<pre class="brush: python; gutter: false; title: ; notranslate">
count(0)
0
1
2
...
998
RuntimeError: maximum recursion depth exceeded
</pre>
<p>The above code should be pretty self-explanatory, but I&#8217;ll explain it a little bit anyway.  The function <strong>count</strong> has only two instructions.  First print <strong>n</strong> to the screen, then call <strong>count</strong> with <strong>n + 1</strong> as the argument.  So when we call <strong>count(0)</strong> it should print 0 then call <strong>count(1)</strong> which will print 1 then call <strong>count(2)&#8230;</strong></p>
<p>You probably noticed the error in the above output.  We&#8217;re going to ignore that for the moment and move on from trying to count to infinity.  Let&#8217;s just count to 5.  To tell when we&#8217;ve hit 5 we&#8217;ll need to check the value of <strong>n</strong> each time <strong>count</strong> is called.</p>
<pre class="brush: python; title: ; notranslate">
def count(n):
  if n &lt; 5:
    print n
    count(n+1)
</pre>
<pre class="brush: python; gutter: false; title: ; notranslate">
count(5)
0
1
2
3
4
</pre>
<p>As you can see from these examples, a recursive function is simply a function that calls itself.  Let&#8217;s look at another recursive solution to counting.  This time we&#8217;re going to use a list of the numbers 0 through 4 and print each number in the list.</p>
<pre class="brush: python; title: ; notranslate">
def print_list(lst):
  if lst:
    print lst[0]
    print_list([1:])
</pre>
<pre class="brush: python; gutter: false; title: ; notranslate">
print_list(range(5))
0
1
2
3
4
</pre>
<p>In this code we print the first element in the list, then call <strong>print_list</strong> with the rest of that list from the 2nd element on.  Here&#8217;s a diagram of the process to make it a bit more clear.</p>
<p><img src="https://docs.google.com/drawings/pub?id=1wMO1ZSdpH6GFSWTl4XM_lGdIkDXzdjcDw9dZHjvHcKg&amp;w=257&amp;h=471"></p>
<p>When writing recursive code it is important to include a stopping condition, which was the empty list in the code above.  If you don&#8217;t include a stopping condition, your code will enter an infinite loop.</p>
<p>&nbsp;</p>
<h2>Tail-Call Optimization</h2>
<p>One problem with recursive code is that most programming languages translate recursive code into something very similar to the diagram above.</p>
<p>Let&#8217;s go back to trying to print to infinity again and look at the error when we run the code.  Depending on how many lines your terminal logs you may not see this.</p>
<pre class="brush: python; gutter: false; title: ; notranslate">
993
994
995
996
997
998
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;&lt;stdin&gt;&quot;, line 3, in print_infinity
  File &quot;&lt;stdin&gt;&quot;, line 3, in print_infinity
  File &quot;&lt;stdin&gt;&quot;, line 3, in print_infinity
  File &quot;&lt;stdin&gt;&quot;, line 3, in print_infinity
  ...
  RuntimeError: maximum recursion depth exceeded
</pre>
<p>Since most languages, Python included, see recursive code exactly like the diagram above, try to imagine how big our diagram would have to be to represent a 1,000!  Eventually the language simply runs out of memory to store this huge diagram or it hits a set limit to prevent that from happening.</p>
<p>Let&#8217;s take a look at another example, <strong>countdown</strong>.  This function simply counts down from a number, <strong>n</strong> to 1.</p>
<pre class="brush: python; title: ; notranslate">
def countdown(n):
  if n:
    print n
    countdown(n-1)
</pre>
<pre class="brush: python; gutter: false; title: ; notranslate">
countdown(5)
5
4
3
2
1
</pre>
<p>Each time Python sees <strong>countdown(n-1)</strong> it makes a new function call.  Because Python is <a href="http://en.wikipedia.org/wiki/Scope_%28programming%29#Lexical_scoping">lexically scoped</a>, the function call creates a new frame to hold the bindings and arguments.  When the next <strong>countdown(n-1)</strong> is evaluated a new frame is created inside the last frame.  Then again, and again, until it looks like the code below, each new level of indentation is a new frame within the previous frame.</p>
<pre class="brush: python; gutter: false; title: ; notranslate">
countdown(5)
  print 5
  countdown(4)
    print 4
    countdown(3)
      print 3
      countdown(2)
        print 2
        countdown(1)
          print 1
          countdown(0)
</pre>
<p>From looking at the code above, it doesn&#8217;t make much sense why any language would do this.  However if you switch the code for <strong>countdown</strong> around a little bit, it starts to make sense.</p>
<pre class="brush: python; title: ; notranslate">
def countdown(n):
  if n:
    countdown(n-1)
    print n
</pre>
<p>All we did was switch the <strong>countdown</strong> and <strong>print</strong> lines, but let&#8217;s see how that changes the execution of this function.</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
countdown(5)
1
2
3
4
5
</pre>
<p>This version of <strong>countdown</strong> doesn&#8217;t countdown at all!  In fact it counts up.  To see why, let&#8217;s examine the execution of this function.</p>
<pre class="brush: python; gutter: false; title: ; notranslate">
countdown(5)
  countdown(4)
    countdown(3)
      countdown(2)
        countdown(1)
          countdown(0)
          print 1
        print 2
      print 3
    print 4
  print 5
</pre>
<p>And a longer, but more descriptive version&#8230;</p>
<pre class="brush: python; collapse: true; gutter: false; light: false; title: ; toolbar: true; notranslate">
countdown(5)
  if 5:
    countdown(5 - 1)
      if 4:
        countdown(4 - 1)
          if 3:
            countdown(3 - 1)
              if 2:
                countdown(2 - 1)
                  if 1:
                    countdown(1 - 1)
                    print 1
                print 2
            print 3
        print 4
    print 5
</pre>
<p>In this scenario there is a <strong>print</strong> instruction after the call to <strong>countdown</strong>.  This means that the language <strong>must evaluate countdown before printing the number</strong>.  The only way to do this is to create a tree structure as depicted above and keep it all in memory until the function has finished executing.</p>
<p>Some languages like Scheme perform <strong>tail-call optimization (TCO)</strong>, also known as <strong>tail-call elimination</strong>.  If the recursive function call is the last thing you do in the function body, there cannot be anymore instructions to perform after the call.  Because it is guaranteed that none of the information from the initial frame is needed, there is no reason to keep it around by nesting new frames in it.  In other words tail-call optimization transforms a recursive definition into an iterative one.</p>
<p>Language support for tail-call optimization varies widely.  Some languages, like Scheme, guarantee that recursive function calls in the tail position will run in constant space.  While on the lower end of the spectrum is a compiler like gcc that <strong>may</strong> run tail calls in constant space, even though the C standard does not support TCO.</p>
<p>Languages that guarantee tail-call optimization generally use recursion as the main form of looping.  Because of this function calls in these languages are usually highly optimized.</p>
<p>Here are tail-call optimized versions of count and countdown in Scheme.  Notice how the recursive calls are the last calls that are executed in the body.</p>
<pre class="brush: plain; title: ; notranslate">
(define (count n)
  (print n)
  (count (+ n 1)))

(define (countdown n)
  (if (= n 0)
    (print n)
    (begin
      (print n)
      (countdown (- n 1)))))
</pre>
<pre class="brush: plain; title: ; notranslate">
(countdown 3)
3
2
1
0
(count 0)
1
2
3
...
</pre>
<p>&nbsp;</p>
<h2>Mutually Recursive Functions</h2>
<p>This is the classic example of a mutually recursive function.  These two functions together will determine whether a number is even or odd.  Take a minute to look at the code and try to figure out for yourself how it works.</p>
<pre class="brush: python; title: ; notranslate">
def even(n):
  if n == 0:
    return True
  else:
    return odd(n-1)

def odd(n):
  if n == 0:
    return False
  else:
    return even(n-1)
</pre>
<pre class="brush: python; gutter: false; title: ; notranslate">
even(4)
#===&gt; True
odd(4)
#===&gt; False
odd(3)
#===&gt; True
</pre>
<p>Because everyone likes a good sports analogy let’s imagine two kids playing catch.  One kid is named Even and the other is named Odd.  Even starts with the ball in his hand and they throw the ball back and forth until they lose count.  If Even has the ball last, they threw the ball an even number of times.  If Odd has the ball last, they threw the ball an odd number of times.  When they finish playing, the kid with the ball screams “I got it!” and the game ends.</p>
<p>The mutually recursive function above works the same way.  <strong>even</strong> subtracts 1 from the number and throws it to <strong>odd</strong>.  <strong>odd</strong> also subtracts 1 from the number and throws it back to <strong>even</strong>.  When the number reaches 0 the last function to have the number returns, #t if <strong>even</strong> is the last to have it and #f if <strong>odd</strong> is the last to have it.</p>
<p>We’ve already learned that a recursive function creates a loop by calling itself.  Mutually recursive functions create a loop by calling each other.  Like recursive functions, mutually recursive functions must check for some condition to prevent an infinite loop.</p>
<p><a href="http://nickzarr.com/blog4/what-in-the-hell-series/">GOTO Table of Contents</a></p>
]]></content:encoded>
			<wfw:commentRss>http://nickzarr.com/blog4/2011/03/what-in-the-hell-is-recursion/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>What In The Hell Are Errors</title>
		<link>http://nickzarr.com/blog4/2011/02/what-in-the-hell-are-errors/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-in-the-hell-are-errors</link>
		<comments>http://nickzarr.com/blog4/2011/02/what-in-the-hell-are-errors/#comments</comments>
		<pubDate>Fri, 25 Feb 2011 02:04:08 +0000</pubDate>
		<dc:creator>Nick Zarczynski</dc:creator>
				<category><![CDATA[What In The Hell Series]]></category>
		<category><![CDATA[bugs]]></category>
		<category><![CDATA[chicken scheme]]></category>
		<category><![CDATA[comp sci]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[errors]]></category>
		<category><![CDATA[Learn To Program]]></category>
		<category><![CDATA[learning programming]]></category>
		<category><![CDATA[programming bugs]]></category>
		<category><![CDATA[programming errors]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Scheme]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[What In The Hell]]></category>
		<category><![CDATA[what in the hell series]]></category>
		<category><![CDATA[with]]></category>

		<guid isPermaLink="false">http://nickzarr.com/blog4/?p=282</guid>
		<description><![CDATA[GOTO All What In The Hell Articles Almost every program you will ever write will have errors. As soon as we started programming, we found out to our surprise that it wasn&#8217;t as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I [...]]]></description>
			<content:encoded><![CDATA[<p><a title="What In The Hell Series" href="http://nickzarr.com/blog4/what-in-the-hell-series/">GOTO All What In The Hell Articles</a></p>
<p>Almost every program you will ever write will have errors.</p>
<blockquote><p>As soon as we started programming, we found out to our surprise that it wasn&#8217;t as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.<br />
<a href="http://www.quoteland.com/author/Maurice-Wilkes-Quotes/88/">-Maurice Wilkes</a></p></blockquote>
<p>&nbsp;</p>
<h2>Syntax Errors</h2>
<p>Syntax errors are probably the most common errors in programming.  Luckily they are usually also the easiest to fix.  Most languages will point out syntax errors as soon as you try to run or compile your program.  Usually you will be provided with a file and line number of the offending syntax.  Some IDEs check your syntax as you type, providing a sort of spell checker for syntax.</p>
<p><strong>Python</strong></p>
<pre class="brush: python; title: ; notranslate">
def add(x, y)
  return x + y
</pre>
<pre class="brush: plain; title: ; notranslate">
File &quot;./test.py&quot;, line 2
  def add(x, y)
              ^
SyntaxError: invalid syntax
</pre>
<p><strong>Chicken Scheme</strong></p>
<pre class="brush: plain; title: ; notranslate">
(define add(x y) (+ x y))
</pre>
<pre class="brush: plain; title: ; notranslate">
Error: during expansion of (define ...) - in `define' - too many arguments: (define add (x y) (+ x y))

        Call history:

        &lt;syntax&gt;          (define add (x y) (+ x y))
</pre>
<p>&nbsp;</p>
<h2>Type Errors</h2>
<p>Type errors occur when your code tries to do things like adding an integer and a string together.  Depending on the language you use you may be notified of type errors when compiling your program or while your program is running.  Type errors are also very common and are a little bit harder to fix.</p>
<p><strong>Python</strong></p>
<pre class="brush: python; title: ; notranslate">
def add(x, y):
  return x + y
add(1, &quot;a&quot;)
</pre>
<pre class="brush: plain; title: ; notranslate">
Traceback (most recent call last):
  File &quot;./test.py&quot;, line 4, in &lt;module&gt;
    add(1, &quot;a&quot;)
  File &quot;./test.py&quot;, line 3, in add
    return x + y
TypeError: unsupported operand type(s) for +: 'int' and 'str'
</pre>
<p><strong>Chicken Scheme</strong></p>
<pre class="brush: plain; title: ; notranslate">
(+ 1 &quot;a&quot;)
</pre>
<pre class="brush: plain; title: ; notranslate">
Error: (+) bad argument type: &quot;a&quot;

        Call history:

        &lt;syntax&gt;          (+ 1 &quot;a&quot;)
        &lt;eval&gt;    (+ 1 &quot;a&quot;)     &lt;--
</pre>
<p>&nbsp;</p>
<h2>Logical Errors</h2>
<p>Logical errors occur when you write code that performs correctly, but does not give the output that you desire.  Logical errors can be the worst kind of bugs.  There is rarely any support for detecting them built into the language, as there is technically nothing wrong with the code.  These bugs happen somewhat frequently and can range from minor inconvenience to major disruption.</p>
<p>Below is an example of a logical error that would fall into the minor inconvenience category.  We&#8217;re trying to define a function named add that takes two arguments, adds them together and returns the result.  The code we have below does not have any syntax or type errors in it and it is perfectly valid code.  However instead of adding the two arguments it subtracts them and we get the answer 0 when we expected the answer to be 4.</p>
<p><strong>Python</strong></p>
<pre class="brush: python; title: ; notranslate">
def add(x, y):
  return x - y
add(2, 2)
#===&gt; 4
</pre>
<pre class="brush: plain; title: ; notranslate">
0
</pre>
<p><strong>Chicken Scheme</strong></p>
<pre class="brush: plain; title: ; notranslate">
(define (add x y)
  (- x y))
(add 2 2)
;===&gt; 4
</pre>
<pre class="brush: plain; title: ; notranslate">
0
</pre>
<p>&nbsp;</p>
<h2>Errors and Error Handling</h2>
<p>Each language will have its own way of representing errors.  Some languages provide a mechanism called error handling which let&#8217;s you control what happens when an error occurs.  We&#8217;ll get deeper into errors and error handling in an upcoming WITH article.</p>
<p><a title="What In The Hell Series" href="http://nickzarr.com/blog4/what-in-the-hell-series/">GOTO Table of Contents</a></p>
]]></content:encoded>
			<wfw:commentRss>http://nickzarr.com/blog4/2011/02/what-in-the-hell-are-errors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What In The Hell Is Big O</title>
		<link>http://nickzarr.com/blog4/2011/02/what-in-the-hell-is-big-o/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-in-the-hell-is-big-o</link>
		<comments>http://nickzarr.com/blog4/2011/02/what-in-the-hell-is-big-o/#comments</comments>
		<pubDate>Thu, 24 Feb 2011 06:40:13 +0000</pubDate>
		<dc:creator>Nick Zarczynski</dc:creator>
				<category><![CDATA[What In The Hell Series]]></category>
		<category><![CDATA[big O]]></category>
		<category><![CDATA[big O notation]]></category>
		<category><![CDATA[comp sci]]></category>
		<category><![CDATA[compsci]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[learn programming]]></category>
		<category><![CDATA[Learn To Program]]></category>
		<category><![CDATA[learning programming]]></category>
		<category><![CDATA[programming basics]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Scheme]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[What In The Hell]]></category>

		<guid isPermaLink="false">http://nickzarr.com/blog4/?p=204</guid>
		<description><![CDATA[GOTO All What In The Hell Articles Big O is a notation for describing the worst-case performance of an algorithm or procedure. This post will show real-world examples of big O notation. &#160; O(1) &#8211; Constant Time A constant time algorithm will always take the same amount of time to run. Example: Finding a book [...]]]></description>
			<content:encoded><![CDATA[<p><a title="What In The Hell Series" href="http://nickzarr.com/blog4/what-in-the-hell-series/">GOTO All What In The Hell Articles</a></p>
<p>Big O is a notation for describing the worst-case performance of an algorithm or procedure.  This post will show real-world examples of big O notation.</p>
<p>&nbsp;</p>
<h2 id="constant">O(1) &#8211; Constant Time</h2>
<p>A constant time algorithm will always take the same amount of time to run.</p>
<p><img src="https://docs.google.com/drawings/pub?id=1mF1jw_uES-JJBvL4CfI0pQeNwfaZbSnS0m8GAmfEv6E&amp;w=664&amp;h=317" alt="" /></p>
<p><strong>Example:</strong><br />
Finding a book in a library.</p>
<p><strong>Why:</strong><br />
There are many methods to finding a book in a library, but I&#8217;m going to stick with the simplest, ask the librarian.  Assuming the librarian is familiar with their library, it should take about the same amount of time to find a book about mathematics as it would about poetry.</p>
<p>For another example see <a title="What In The Hell Are Arrays" href="http://nickzarr.com/blog4/2011/02/20/what-in-the-hell-are-arrays/#index">indexing an array</a>.</p>
<p>&nbsp;</p>
<h2 id="linear">O(n) &#8211; Linear Time</h2>
<p>A linear algorithm runs once for each item(n).</p>
<p><img src="https://docs.google.com/drawings/pub?id=1cjwfSJ1mFF6XquMTtsoj90ZIhig0cdiSqeLEEQR9nVM&amp;w=663&amp;h=307" alt="" /></p>
<p><strong>Example:</strong><br />
Finding a CD in a stack of CDs</p>
<p><strong>Why:</strong><br />
The simplest method of finding a CD in a stack of CDs is to just look at the one on top.  If that CD is the one you&#8217;re looking for, you&#8217;ve found it.  If it is not, look at the next one.  Repeat until there are no CDs.  If the CD we&#8217;re looking for is the last one in the stack, we had to look at every CD to find it.</p>
<p>But what about all the times that you look for a CD and the CD you&#8217;re looking for is in the middle or on the top?  Big O notation describes the worst-case running time of your algorithm.  It assumes that every search will be for the last CD (or a CD that doesn&#8217;t exist).  Big O is not a statistical model of how often a given condition will occur in an algorithm!</p>
<p>&nbsp;</p>
<h2 id="logarithmic">O(log n) &#8211; Logarithmic Time</h2>
<p>A logarithmic algorithm runs log<sub>2</sub>(n) times.</p>
<p><strong>Example:</strong><br />
Let&#8217;s play a guessing game.  I&#8217;m going to pick a number between 0 and 100 and you have to guess it.  You have to try to guess the number in as few guesses as possible.  Every time you guess I will tell you if my number is higher or lower.</p>
<p><strong>Why:</strong><br />
You could devise a number of strategies for playing this game, however one strategy is particularly good at this type of game, binary search.  Each time we guess, we guess the number right in the middle.  By doing this we halve the number of possibilities every time, let&#8217;s play a little game&#8230;</p>
<p><img src="https://docs.google.com/drawings/pub?id=137q2qaUaTSEGQG8M1lsykpzOpkyIQCxY34pBKXS6RZA&amp;w=664&amp;h=1196" alt="" /></p>
<p>The game above plays out the worst case performance of an O(log n) algorithm like binary search.  Instead of making 100 guesses at worst, we only make 7!</p>
<p>&nbsp;</p>
<h2 id="quadratic">O(n<sup>2</sup>) &#8211; Quadratic Time</h2>
<p>A quadratic time algorithm runs n<sup>2</sup> times, or n times for each n.</p>
<p><img src="https://docs.google.com/drawings/pub?id=1i9HLXG_5YWXWtb4QPLuqXnnaDWC3GbC7Ymvr652cuqI&amp;w=666&amp;h=433" alt="" /></p>
<p><strong>Example:</strong><br />
Check if there are any duplicates in a deck of cards.</p>
<p><strong>Why:</strong><br />
The simplest way to check if there are any duplicate cards in a deck is to pick the first card from the deck.  Then compare that card to every other card in the deck.  If there are no matches take the second card from the deck.  Then compare the second card to every other card in the deck.  Continue until you have checked all the cards.</p>
<p>By the time we are done we have looked at each card in the deck 52 times for a total of (52 * 52) 2704 comparisons.  There are ways to lessen this number, like not looking through cards that you have already compared.  However the most important thing to remember with big O notation is that it always describes the worst-case time, not the average.</p>
<p>Here&#8217;s what the process looks like if you don&#8217;t re-check cards you&#8217;ve already compared.</p>
<p><img src="https://docs.google.com/drawings/pub?id=1cGBI5860T8rLtQddDZxZewkBkcGRJR1wA_-3vPkRW-E&amp;w=665&amp;h=418" alt="" /></p>
<p>I&#8217;ll come back later and add some more big O notations, so check back.  For now I&#8217;ll leave you with a question to ponder.  What is the big O notation of the diagram above?</p>
<h2>Links</h2>
<p><strong>Sorting</strong> (this will be the topic of another WITH article, but for now&#8230;)</p>
<p><a href="http://jsdo.it/norahiko/oxIy/fullscreen">Visual Representation of Sorting Algorithms in Javascript</a></p>
<p><a href="http://corte.si/posts/code/visualisingsorting/index.html">Visualizing Sorting Algorithms</a></p>
<p><a href="http://sortvis.org/">sortvis</a></p>
<p><a href="http://www.cse.chalmers.se/~uffe/hybridsort.pdf">Parallel GPU Sorting (pdf)</a></p>
<p><a href="http://www.youtube.com/watch?v=t8g-iYGHpEA">What different sorting algorithms sound like</a></p>
<p><a title="What In The Hell Series" href="http://nickzarr.com/blog4/what-in-the-hell-series/">GOTO Table of Contents</a></p>
]]></content:encoded>
			<wfw:commentRss>http://nickzarr.com/blog4/2011/02/what-in-the-hell-is-big-o/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What In The Hell Are Trees</title>
		<link>http://nickzarr.com/blog4/2011/02/what-in-the-hell-are-trees/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-in-the-hell-are-trees</link>
		<comments>http://nickzarr.com/blog4/2011/02/what-in-the-hell-are-trees/#comments</comments>
		<pubDate>Mon, 21 Feb 2011 01:14:00 +0000</pubDate>
		<dc:creator>Nick Zarczynski</dc:creator>
				<category><![CDATA[What In The Hell Series]]></category>
		<category><![CDATA[collections]]></category>
		<category><![CDATA[comp sci]]></category>
		<category><![CDATA[compsci]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[data structure]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[data type]]></category>
		<category><![CDATA[data types]]></category>
		<category><![CDATA[datatype]]></category>
		<category><![CDATA[datatypes]]></category>
		<category><![CDATA[Learn To Program]]></category>
		<category><![CDATA[learning programming]]></category>
		<category><![CDATA[programming basics]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Scheme]]></category>
		<category><![CDATA[sequences]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[What In The Hell]]></category>

		<guid isPermaLink="false">http://nickzarr.com/blog4/?p=195</guid>
		<description><![CDATA[GOTO All What In The Hell Articles A tree is a hierarchical data structure that contains a set of nodes. The node is the the basic building block of a tree and to understand trees you have to understand nodes. A node is a data structure that has a parent, can contain a value and [...]]]></description>
			<content:encoded><![CDATA[<p><a title="What In The Hell Series" href="http://nickzarr.com/blog4/what-in-the-hell-series/">GOTO All What In The Hell Articles</a></p>
<p>A tree is a hierarchical data structure that contains a set of nodes.  The node is the the basic building block of a tree and to understand trees you have to understand nodes.</p>
<p>A node is a data structure that has a parent, can contain a value and can have 0 or more child nodes.  A node with no child nodes is called a leaf node, or more commonly just a leaf.</p>
<p><img src="https://docs.google.com/drawings/pub?id=13hcL4zVu-FLbggbqxaQWe5Mg3rUkD5MGk5pnClKLVoU&amp;w=262&amp;h=187" alt="" /></p>
<p>A node with no parent is referred to as a root node.</p>
<p><img src="https://docs.google.com/drawings/pub?id=1FA5WtjehyYmnMthTlliK0cId0Ci6GDrUzGEis9xrewE&amp;w=348&amp;h=356" alt="" /></p>
<p>Each node can have a child node of its own.</p>
<p><img src="https://docs.google.com/drawings/pub?id=1RmHqLORBa_z5p4VjWv6wGfa306vzKvxWm-2zTV5SDuo&amp;w=454&amp;h=511" alt="" /></p>
<p>A node can have more than two children too.</p>
<p><img src="https://docs.google.com/drawings/pub?id=1R_YsYFm1WFg3LspHCa0U6Tq6uhU_VE3lKMZ25uxk7z4&amp;w=454&amp;h=511" alt="" /></p>
<p>By now you should grasp the basic structure of a tree, but what are they good for and where are they used?  Well the good news is that trees are all around you.  Your operating system&#8217;s directory structure is a tree, HTML and XML are both trees.  Your filing cabinet could even be considered a tree.  A <a title="What In The Hell Are Linked Lists" href="http://nickzarr.com/blog4/2011/02/20/what-in-the-hell-are-linked-lists/">linked list</a> is also a form of tree, where each node has exactly one child node.  Let&#8217;s take a look at some more trees.</p>
<p><a href="http://nickzarr.com/blog4/wp-content/uploads/2011/02/tree_example1.jpg"><img class="alignnone size-full wp-image-374" title="tree_example1" src="http://nickzarr.com/blog4/wp-content/uploads/2011/02/tree_example1.jpg" alt="" width="455" height="307" /></a></p>
<p>You belong to a tree too!</p>
<p><a href="http://nickzarr.com/blog4/wp-content/uploads/2011/02/tree_example_boss.gif"><img class="alignnone size-full wp-image-375" title="tree_example_boss" src="http://nickzarr.com/blog4/wp-content/uploads/2011/02/tree_example_boss.gif" alt="" width="516" height="336" /></a></p>
<p>and of course&#8230;</p>
<p><a href="http://nickzarr.com/blog4/wp-content/uploads/2011/02/tree_example_tree.png"><img class="alignnone size-full wp-image-376" title="tree_example_tree" src="http://nickzarr.com/blog4/wp-content/uploads/2011/02/tree_example_tree.png" alt="" width="550" height="720" /></a></p>
<p>&nbsp;</p>
<h2>Implementation of Trees</h2>
<p>Programming languages have varying levels of support for tree-like data structures.  The abstract idea of a tree and the implementation of one are often very different things.  Pretty much any nestable data structure can suffice for an ad-hoc tree implementation.  For example here&#8217;s one of the simplest trees you can implement in Python.</p>
<pre class="brush: python; title: ; notranslate">
class Node:
  def __init__(self, value=None, *args):
    self.value = value
    self.children = args

# A tree can then be defined as
tree = Node('root', Node(1), Node(2))
</pre>
<p>Scheme makes it quite a bit easier, both code and data are represented using trees (actually lists).</p>
<pre class="brush: plain; title: ; notranslate">
(define tree (cons 1 2))
tree
;===&gt; (1 . 2)

(define nested-tree (cons 1 (cons 2 3)))
nested-tree
;===&gt; (1 2 . 3)
</pre>
<p>If you&#8217;re wondering why the nested-tree looks like a list, remember that lists are a subset of trees.</p>
<p>I didn&#8217;t get to heavily into how trees are implemented in any particular language.  Mostly this is because the implementations of tree data structures varies widely from one language to the next and sometimes there are even multiple implementations of trees in a single language.</p>
<p>Many decisions go into a tree implementation and the tools that work with them.   Because of this there are many different types of trees, each with its own strengths and weaknesses and tools for manipulating them.  Future WITH articles will will go deeper into trees, their implementations and characteristics.</p>
<p><a title="What In The Hell Series" href="http://nickzarr.com/blog4/what-in-the-hell-series/">GOTO Table of Contents</a></p>
]]></content:encoded>
			<wfw:commentRss>http://nickzarr.com/blog4/2011/02/what-in-the-hell-are-trees/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What In The Hell Are Functions</title>
		<link>http://nickzarr.com/blog4/2011/02/what-in-the-hell-are-functions/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-in-the-hell-are-functions</link>
		<comments>http://nickzarr.com/blog4/2011/02/what-in-the-hell-are-functions/#comments</comments>
		<pubDate>Sun, 20 Feb 2011 23:11:57 +0000</pubDate>
		<dc:creator>Nick Zarczynski</dc:creator>
				<category><![CDATA[What In The Hell Series]]></category>
		<category><![CDATA[comp sci]]></category>
		<category><![CDATA[compsci]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Learn To Program]]></category>
		<category><![CDATA[learning programming]]></category>
		<category><![CDATA[programming basics]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Scheme]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[What In The Hell]]></category>

		<guid isPermaLink="false">http://nickzarr.com/blog4/?p=186</guid>
		<description><![CDATA[GOTO All What In The Hell Articles A good way to think about functions is as machines. Machines are designed to repeat a process over and over again. Functions share a lot of characteristics with machines, so let&#8217;s look at some of the properties of machines we&#8217;re already familiar with. Machines take input and produce [...]]]></description>
			<content:encoded><![CDATA[<p><a title="What In The Hell Series" href="http://nickzarr.com/blog4/what-in-the-hell-series/">GOTO All What In The Hell Articles</a></p>
<p>A good way to think about functions is as machines.  Machines are designed to repeat a process over and over again.  Functions share a lot of characteristics with machines, so let&#8217;s look at some of the properties of machines we&#8217;re already familiar with.</p>
<p>Machines take input and produce output, let&#8217;s take a look at a few examples of that.  Your dish washer takes dirty dishes as input and returns clean dishes as output, your washer does the same for clothes.  Your radio takes radio waves as input and produces music on your speakers as output.  </p>
<p>For the most part you don&#8217;t have to know how a machine works, you only need to know how to use it.  For the moment we&#8217;re going to look at functions the same way, not thinking about how they work, just about how to use them.  You&#8217;ll be happy to learn that you&#8217;re already familiar with a few functions, in fact here&#8217;s one you use every day.</p>
<p><img src="https://docs.google.com/drawings/pub?id=1rU2tfp48hljoEOMXoDeZD22r-97zjZW7DZY6er-9DoI&amp;w=396&amp;h=193"></p>
<p>Using the function above is simple.  We don&#8217;t care how it works, whether it is carrying it&#8217;s ones or counting on its fingers.  The only thing that matters to us is that we give it 2 numbers, 35 and 7 and it gives us back 42.</p>
<p>Here&#8217;s an example of another function.  This one reverses any string that we give it.</p>
<p><img src="https://docs.google.com/drawings/pub?id=17mWHCgNqbjld7dBJbWBZyKynG7iyrC68lkAwAeDxWzE&amp;w=396&amp;h=193"></p>
<p>&nbsp;</p>
<h2>Terminology</h2>
<p>When we use a function it is referred to as <strong>calling that function</strong>.  So in the above two examples we <strong>called the add function</strong> and we also <strong>called the reverse function</strong>.</p>
<p>The input to a function is referred to as the <strong>arguments</strong> or <strong>argument</strong> in the case of a single input.  Adding arguments to the above we get, we <strong>called the add function with the arguments 35 and 7</strong> and we also <strong>called the string function with Hello as an argument</strong>.  Arguments are also commonly referred to as <strong>parameters</strong> and the two words can be used interchangeably.</p>
<p>The output of a function is most commonly referred to as the <strong>return value</strong>, though it is referred to as <strong>output</strong> as well.</p>
<p>&nbsp;</p>
<h2>Calling Functions</h2>
<p>Now that we&#8217;ve explored some of the properties and terminology of functions let&#8217;s look at how they&#8217;re represented in code.  Just like before, we are not going to worry about how the functions work just yet.  We&#8217;re only going to call a few.</p>
<p>Function calls can be represented a number of ways, depending on the language you are using.  However the vast majority of mainstream languages use this convention.</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
____________( ______, ______ )
</pre>
<p>Instead of a fill-in-the-blanks style as above, programmers usually use a shorthand for describing things that can be anything.  Unfortunately there is no standardization for this shorthand, so to know what is a shorthand you need to know what words are never used as real names.  The use of <strong>foo</strong>, <strong>bar</strong> and <strong>baz</strong> are commonplace and you will have to get accustomed to reading them eventually.</p>
<p>I will take another approach here.  I&#8217;ll use prototypes to talk about the basic structure and I&#8217;ll use descriptive names in any code.  This approach is used in many Scheme manuals and I have found it easy to grasp.  For now a prototype will be a source code snippet with no line numbering and all the lines highlighted.</p>
<p>Lets start our introductions to prototypes with a prototype of the add function.</p>
<p><strong>Prototype:</strong></p>
<pre class="brush: python; gutter: false; highlight: [1]; title: ; notranslate">
name(argument, argument)
</pre>
<p>Let&#8217;s look at the diagram for add and put these labels on it in red.</p>
<p><img src="https://docs.google.com/drawings/pub?id=1EG8PueMpE_KFOMWMODLmEB7kMohLhnay5-zxJbo2dO0&amp;w=396&amp;h=193"></p>
<p>&nbsp;</p>
<h2>Function Arguments</h2>
<p>Functions often take arguments of a specific type.  For example the add function above only takes numbers.  If you attempt to give it a <a href="http://nickzarr.com/blog4/2011/02/20/what-in-the-hell-are-strings/">string</a> it will produce an <a href="http://nickzarr.com/blog4/2011/02/24/what-in-the-hell-are-errors/">error</a>.  In contrast the reverse function will only accept a string.</p>
<p>To make it easier for you to tell what type of argument to use, the prototypes will state the type.</p>
<pre class="brush: plain; gutter: false; highlight: [1,2,3]; title: ; notranslate">
add(number, number)
reverse(string)
length(argument)
</pre>
<p>Whenever the type of the argument does not matter or can be of multiple types the word <strong>argument</strong> will be used.  </p>
<p>Let&#8217;s take a look at a few more functions and how calling them maps out onto our diagram.  The first function we&#8217;ll look at is <strong>max</strong> it will take two numbers as input and give us back the largest one.  First I&#8217;ll show you the prototype then the code used to call <strong>max</strong> followed by the diagram.</p>
<p><strong>Prototype:</strong></p>
<pre class="brush: plain; gutter: false; highlight: [1]; title: ; notranslate">
max(number, number)
</pre>
<pre class="brush: python; title: ; notranslate">
max(35, 7)
#===&gt; 35
</pre>
<p><img src="https://docs.google.com/drawings/pub?id=1ikFeSFIkJUkr45t8qs4xsO_LmpnfZwbYXgDyF8LLgXs&amp;w=396&amp;h=193"></p>
<p>You probably noticed the #===> symbol in the source code.  # is a comment in Python, which is the language used for the psudo-code in this series.  A comment is completely disregarded by the computer and is only meant for you to read.  This series uses the ===> symbol to show you the output of the code that was just run.</p>
<p>Our next example is <strong>length</strong> which takes a list or string as input and produces the length of that list or string as output.  Because it takes a list or a string, we&#8217;ll use <strong>argument</strong> in our prototype.</p>
<p><strong>Prototype:</strong></p>
<pre class="brush: plain; gutter: false; highlight: [1]; title: ; notranslate">
length(argument)
</pre>
<pre class="brush: python; title: ; notranslate">
length(&quot;Hello&quot;)
</pre>
<p><img src="https://docs.google.com/drawings/pub?id=176JMo1xSSkhE_pAdPRXHrMjHuQla5Q4QLobpYnKZZmA&amp;w=396&amp;h=193"></p>
<p>Functions can take two different kinds of arguments too.  Here&#8217;s a function that takes a string and a number as input and gives us back a letter.  Its output is the same as finding the letter by starting at the beginning and counting each letter until you get to <strong>number</strong> then return that letter.  </p>
<p><strong>Prototype:</strong></p>
<pre class="brush: plain; gutter: false; highlight: [1]; title: ; notranslate">
letter(string, number)
</pre>
<pre class="brush: python; title: ; notranslate">
letter(&quot;Hello&quot;, 2)
#===&gt; e
</pre>
<p><img src="https://docs.google.com/drawings/pub?id=1CVDc4JUr8gvY1aS4Ab4jjzaD8y8uWrahxTvJPCG-64I&amp;w=396&amp;h=193"></p>
<p>&nbsp;</p>
<h2> Writing Your Own Functions</h2>
<p>I&#8217;m going to borrow from SICP here and start with a simple function called <strong>squared</strong>.  This function takes one number as input and returns the result of multiplying that number by itself.</p>
<p>The first thing we&#8217;ll do is define what this function should do.  You will most likely do this in your head, but here I&#8217;ll lay it out like I laid out the functions above.  We know we need a number as input and the output should be that number multiplied by itself, so let&#8217;s start with a prototype, a fake call, and a diagram like before.</p>
<p><strong>Prototype:</strong></p>
<pre class="brush: plain; gutter: false; highlight: [1]; title: ; notranslate">
squared(number)
</pre>
<pre class="brush: python; title: ; notranslate">
squared(4)
#===&gt; 16
</pre>
<p><img src="https://docs.google.com/drawings/pub?id=1UQrhOaqXWCGu7LR3JyFcVK-pHWwrcCW9mLF9SzS_TSk&amp;w=418&amp;h=187"></p>
<p>Now that we know what our function should look like when it&#8217;s finished, we have to define it.  But before we can do that let&#8217;s define what a function is a little more precisely.</p>
<p>&nbsp;</p>
<h2>What is a Function</h2>
<p>When we were using functions we could think of them as &#8220;black boxes&#8221; that we put input into and got output out of.  Now that we&#8217;re writing our own we need to think about how our functions work.  Remember that a machine is made to repeat a process over and over.  Functions are exactly the same, they also repeat a process or series of processes over and over.</p>
<p>A function is both a set of directions for performing a task and a machine that performs that task.</p>
<p>The syntax for defining a function can vary widely between programming languages.  Some languages require that you declare argument and return types.  We&#8217;ll cover that a little bit later, for now we&#8217;re going to focus on the method used by Python.</p>
<p>To define a function in Python you must supply a function name and its arguments.  Here&#8217;s the prototype of Python&#8217;s <strong>def</strong> form, followed by a definition of <strong>squared</strong>.</p>
<p><strong>Prototype:</strong></p>
<pre class="brush: python; gutter: false; highlight: [1,2]; title: ; notranslate">
def name(argument, argument, ...):
  # body
</pre>
<pre class="brush: python; title: ; notranslate">
def squared(number):
  number * number
</pre>
<p>There are two main concepts in the above code that we have not covered yet.  The <strong>body</strong> of a function contains the code that describes the process of the function.  It is in the <strong>body</strong> that the instructions for performing the function are held.  When we call the function we will use the body, but for now it is just a description of the process.</p>
<pre class="brush: python; title: ; notranslate">
squared(4)
</pre>
<p>When we call the function, the programming language looks up the instructions for the function, then replaces all the argument names with the arguments the function is called with.  Here&#8217;s a simple view of what your programming language sees when you call <strong>squared(4)</strong>.</p>
<pre class="brush: python; title: ; notranslate">
def squared(4):
  4 * 4
</pre>
<p>As you can see the <strong>number</strong> argument is replaced by the <strong>4</strong> argument.  Your programming language will do this behind the scenes for you, but you conceptually do this transformation in your head every time you write a function.</p>
<p>However there is one thing wrong with the above function.  When we call it, it doesn&#8217;t give us back the answer.  In fact it seems like it does nothing at all!</p>
<p>&nbsp;</p>
<h2>Returning Values</h2>
<p>Most mainstream languages require the use of a <strong>return</strong> keyword to signal that you want to return a value.  Python also requires a return statement so we&#8217;ll have to add one in to get the <strong>return value</strong>.</p>
<pre class="brush: python; title: ; notranslate">
def squared(number):
  return number * number
squared(4)
#===&gt; 16
</pre>
<p>&nbsp;</p>
<h2>Combining Functions</h2>
<p>Now that we have a function <strong>squared</strong> that will square a number, let&#8217;s look at how to use that in another function.</p>
<p>We&#8217;re going to define a new function <strong>aoc</strong> which will calculate the area of a circle, given a radius as input.  The formula for the area of a circle is &pi;r2.  We already have a way to square the radius with our <strong>squared</strong> function, so let&#8217;s just write the new function.</p>
<pre class="brush: python; title: ; notranslate">
def aoc(radius):
  return 3.14 * squared(radius)
aoc(4)
#===&gt; 50.240000000000002
</pre>
<p>When <strong>aoc(4)</strong> is called Python looks up the definition of <strong>aoc</strong> and replaces all occurrences of <strong>radius</strong> with the number <strong>4</strong>.  Then Python evaluates the instructions found in <strong>aoc</strong>.  In the instructions Python finds a call to <strong>squared</strong> so it looks up the instructions to <strong>squared</strong> and replaces all occurrences of <strong>number</strong> with the value <strong>4</strong>.  It then evaluates the instructions in <strong>squared</strong> and returns <strong>16</strong>.  <strong>squared(4)</strong> is then replaced with <strong>16</strong> and Python returns <strong>3.14 * 16</strong> or roughly <strong>50.24</strong>.</p>
<p>&nbsp;</p>
<h2>Examples of Functions in Other Languages</h2>
<p><strong>Javascript</strong></p>
<pre class="brush: jscript; title: ; notranslate">
function aoc(c) {
  var r = c/2;
  return 3.145*(r*r);
}
aoc(8)
//===&gt; 50.264
</pre>
<p><strong>Scheme</strong></p>
<pre class="brush: plain; title: ; notranslate">
(define (aoc c)
  (define r (/ c 2))
  (* 3.1415 (* r r)))
(aoc 8)
;===&gt; 50.264
</pre>
<p>&nbsp;</p>
<h2>Links</h2>
<p><a title="What In The Hell Series" href="http://nickzarr.com/blog4/what-in-the-hell-series/">GOTO Table of Contents</a></p>
]]></content:encoded>
			<wfw:commentRss>http://nickzarr.com/blog4/2011/02/what-in-the-hell-are-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What In The Hell Are Conditionals</title>
		<link>http://nickzarr.com/blog4/2011/02/what-in-the-hell-are-conditionals/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-in-the-hell-are-conditionals</link>
		<comments>http://nickzarr.com/blog4/2011/02/what-in-the-hell-are-conditionals/#comments</comments>
		<pubDate>Sun, 20 Feb 2011 21:38:10 +0000</pubDate>
		<dc:creator>Nick Zarczynski</dc:creator>
				<category><![CDATA[What In The Hell Series]]></category>
		<category><![CDATA[comp sci]]></category>
		<category><![CDATA[compsci]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Learn To Program]]></category>
		<category><![CDATA[learning programming]]></category>
		<category><![CDATA[programming basics]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Scheme]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[What In The Hell]]></category>

		<guid isPermaLink="false">http://nickzarr.com/blog4/?p=158</guid>
		<description><![CDATA[GOTO All What In The Hell Articles A conditional is a mechanism for altering the control flow of a program based on the result of a test. That is a very technical and complicated description of something that is very simple in practice. Let&#8217;s look at a small example before continuing. All humans are used [...]]]></description>
			<content:encoded><![CDATA[<p><a title="What In The Hell Series" href="http://nickzarr.com/blog4/what-in-the-hell-series/">GOTO All What In The Hell Articles</a></p>
<p>A conditional is a mechanism for altering the control flow of a program based on the result of a test.  That is a very technical and complicated description of something that is very simple in practice.  Let&#8217;s look at a small example before continuing.</p>
<pre class="brush: python; title: ; notranslate">
if temperature &gt; 80:
  run_air_conditioning()
</pre>
<p>All humans are used to dealing with conditional statements, in fact we probably compute thousands of conditionals every day without even thinking about them.  They&#8217;re so in-grained in our thinking that I didn&#8217;t even have to explain the example above, you just got it.</p>
<p>The most basic form of conditional, which is present in almost every language, is if/else.  Each language has slightly different syntax which we&#8217;ll get to in a moment, first let&#8217;s look at some psudo-code that describes the basic workings of if/else.</p>
<pre class="brush: python; title: ; notranslate">
if test:
  do_if_test_is_true()
else:
  do_if_test_is_false()
</pre>
<p>When our programming language comes across this block of code it will first evaluate <strong>test</strong>.  If the evaluation of <strong>test</strong> equals true, the do_if_test_is_true() function will be called.  If the evaluation of <strong>test</strong> equals false, the do_if_test_is_false() function is called.</p>
<p>Note that if test evaluates to true the do_if_test_is_false() function is never called.  This behavior is called branching and a popular way of specifying control flow in a program.</p>
<pre class="brush: python; title: ; notranslate">
if predicate:
  consequent
else:
  alternate
</pre>
<p>These are some common terms that refer to the parts of an if/else.  Predicate refers to the expression or statement that is to be tested.  Consequent refers to the action to be taken if the predicate is equal to true.  Alternate refers to the action to be taken if the predicate is equal to false.</p>
<p>You&#8217;ve already seen examples from Python (even our psudo-code examples were valid Python), here&#8217;s some examples from other languages of if/else.</p>
<p><strong>Javascript</strong></p>
<pre class="brush: jscript; title: ; notranslate">
if (2 &gt; 1) 1
//===&gt; 1

if (1 &gt; 2) {
  1;}
else {
  2;}
//===&gt; 2
</pre>
<p><strong>Scheme</strong></p>
<pre class="brush: plain; title: ; notranslate">
(if (&gt; 2 1) 1)
;===&gt; 1

(if (&gt; 1 2)
    1
    2)
;===&gt; 2
</pre>
<p>&nbsp;</p>
<h2>Else If</h2>
<p>Another common pattern is if/else if/else.  This pattern is equivalent to chaining if/else expressions together.  Here&#8217;s some psudo-code of an if/else if/else in Python and then a diagram of how it is executed.</p>
<pre class="brush: python; title: ; notranslate">
if temp &gt; 80:
  run_ac()
elif temp &lt; 60:
  run_heat()
else:
  do_nothing()
</pre>
<p><img src="https://docs.google.com/drawings/pub?id=1rq5WsQ6Yd2Tto8RJprWg4ebHq8LMZuSAzJKKIWsk71U&amp;w=359&amp;h=250" alt="" /></p>
<p>As you can see, Python first checks if the temperature is greater than 80.  If the temperature is greater than 80, Python will call run_ac() and ignore the rest of the code.  If the temperature is less than 80 it will call the elif branch and check if the temperature is less than 60.  If the temperature is less than 60, Python will call run_heat() and if the temperature is between 60 and 80 Python will call do_nothing().</p>
<p>Instead of using the keyword elif, we could have written our Python code a different way that emphasizes the nesting.</p>
<pre class="brush: python; title: ; notranslate">
if temp &gt; 80:
  run_ac()
else:
  if temp &lt; 60:
    run_heat()
  else:
    do_nothing()
</pre>
<p>Writing our program this way makes it clear that each &#8216;else if&#8217; branch is just another if/else.  However keywords like elif exist for a reason and if you are thinking about nesting your if/else expressions, don&#8217;t.</p>
<p>You&#8217;ve seen elif in Python, now I&#8217;ll show you how some other languages handle it.</p>
<p>&nbsp;</p>
<h2>Functions in Other Languages</h2>
<p><strong>Javascript</strong></p>
<pre class="brush: python; title: ; notranslate">
if (temp &gt; 80) {
  1;}
else if (temp &lt; 60) {
  2;}
else {
  3;}
</pre>
<p><strong>Scheme</strong></p>
<pre class="brush: plain; title: ; notranslate">
(cond ((&gt; temp 80) (run-ac))
      ((&lt; temp 60) (run-heat))
      (else (do-nothing)))
</pre>
<p>As you can see the syntax can vary quite considerably between languages.  However the fundamental concept of the if/else is the same in most programming languages.</p>
<p>&nbsp;</p>
<h2>Switch</h2>
<p>Switch is another form of conditional.  Using the if/else if/else model above is sufficient for a small number of possible conditions.  However as the number of conditions grows the limitations of if/else if/else start to show.</p>
<p>The main problem is that each condition must be checked every time the conditional is evaluated.  If there were 100 conditions each one would have to be checked until one of them was true.  If the condition that returns true is at the end of that list, it could take a lot of time to find it.</p>
<p>Switch statements are usually found in lower-level languages, however I&#8217;ll show you how to implement them in higher-level languages as well (should you need them).  A switch in a lower-level language is often implemented as a <a href="http://en.wikipedia.org/wiki/Branch_table">branch table (or jump table)</a> and can only be done on integers.</p>
<p>Switch in C:</p>
<pre class="brush: cpp; title: ; notranslate">
switch(expression_returning_int) {
  case 1:
    printf(&quot;Expression returned 1.\n&quot;);
    break;
  case 2:
    printf(&quot;Expression returned 2.\n&quot;);
    /* since there is no break; here it will 'fall through' to default. */
  default:
    printf(&quot;Expression returned greater than 1.\n&quot;);
    break;
}
</pre>
<p>In the example above the expression expression_returning_int will be evaluated (it must return an integer).  The value of the expression will then be used as an index into the branch table.  Control is then transfered to the branch referred to by the expression or the default branch (if supplied).</p>
<p>In the example above a break statement was not supplied for the case 2.  If expression_returning_int equals 2 it will print:</p>
<p>Expression returned 2.<br />
Expression returned greater than 1.</p>
<p>The break statement normally stops the execution of that branch.  When it is not present the control flow &#8220;falls through&#8221; to the next branch, and the next, and the next until it hits a break statement or the end of the switch.  This is a common characteristic of switch statements, however it is not universal.  A switch would still be a switch even if it didn&#8217;t allow execution to fall through branches.</p>
<p>&nbsp;</p>
<h2>Switch in Higher-Level Languages</h2>
<p>Many high level languages do not provide a switch statement.  However this is rarely a problem as they often do supply data structures which let us represent one easily.</p>
<p>A switch on integers can be done as an array lookup, if you want to learn more about arrays first go to <a title="What In The Hell Are Arrays" href="http://nickzarr.com/blog4/2011/02/20/what-in-the-hell-are-arrays/">What In The Hell Are Arrays</a>.</p>
<p>If we think about the switch statement above it maps out a little bit like this&#8230;</p>
<p><img src="https://docs.google.com/drawings/pub?id=1cvP7f26Vp_ZWbzL-Chl7ppeB_A8_9QnYoacoAB9uwiA&amp;w=606&amp;h=186" alt="" /></p>
<p>You&#8217;ll notice a problem with the above representation.  Where is the &#8216;default&#8217; branch?  The default branch would be executed by array_lookup if it did not find a value in the array.  The implementation of this will be left as an exercise to the reader as the specifics vary from language to language.</p>
<p>Similarly a switch on strings, or other hash-able values, can be done as a hash lookup.  If you want to learn more about hash tables go to <a title="What In The Hell Are Hash Tables" href="http://nickzarr.com/blog4/2011/02/20/what-in-the-hell-are-hash-tables/">What In The Hell Are Hash Tables</a>.</p>
<p>&nbsp;</p>
<h2>Links</h2>
<p><a title="What In The Hell Series" href="http://nickzarr.com/blog4/what-in-the-hell-series/">GOTO Table of Contents</a></p>
]]></content:encoded>
			<wfw:commentRss>http://nickzarr.com/blog4/2011/02/what-in-the-hell-are-conditionals/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What In The Hell Are Hash Tables</title>
		<link>http://nickzarr.com/blog4/2011/02/what-in-the-hell-are-hash-tables/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-in-the-hell-are-hash-tables</link>
		<comments>http://nickzarr.com/blog4/2011/02/what-in-the-hell-are-hash-tables/#comments</comments>
		<pubDate>Sun, 20 Feb 2011 06:57:35 +0000</pubDate>
		<dc:creator>Nick Zarczynski</dc:creator>
				<category><![CDATA[What In The Hell Series]]></category>
		<category><![CDATA[collections]]></category>
		<category><![CDATA[comp sci]]></category>
		<category><![CDATA[compsci]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[data structure]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[data type]]></category>
		<category><![CDATA[data types]]></category>
		<category><![CDATA[datatype]]></category>
		<category><![CDATA[datatypes]]></category>
		<category><![CDATA[Learn To Program]]></category>
		<category><![CDATA[learning programming]]></category>
		<category><![CDATA[programming basics]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Scheme]]></category>
		<category><![CDATA[sequences]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[What In The Hell]]></category>

		<guid isPermaLink="false">http://nickzarr.com/blog4/?p=135</guid>
		<description><![CDATA[GOTO All What In The Hell Articles A hash table is a combination of two parts; an array, to store the data, and a hash function that computes the index of the array from a key. The best analogy to a hash table is a dictionary. When you use a dictionary you lookup a word [...]]]></description>
			<content:encoded><![CDATA[<p><a title="What In The Hell Series" href="http://nickzarr.com/blog4/what-in-the-hell-series/">GOTO All What In The Hell Articles</a></p>
<p>A  hash table is a combination of two parts; an <a href="http://nickzarr.com/blog4/2011/02/20/what-in-the-hell-are-arrays/">array</a>, to store the data,  and a hash function that computes the index of the array from a key.   The best analogy to a hash table is a dictionary.  When you use a  dictionary you lookup a word (the key) and find the definition (the  value).</p>
<p>Hash  tables are an improvement over arrays and <a href="http://nickzarr.com/blog4/2011/02/20/what-in-the-hell-are-linked-lists/">linked lists</a> for large data  sets.  With a hash table you don’t have to search through every element  in the collection (<a href="http://nickzarr.com/blog4/2011/02/24/what-in-the-hell-is-big-o/#linear">O(n) access time</a>).  Instead you pay a one time cost  (the hash function) to retrieve data (<a href="http://nickzarr.com/blog4/2011/02/24/what-in-the-hell-is-big-o/#constant">O(1) access time</a>).</p>
<p>&nbsp;</p>
<h2>The Hash Function</h2>
<p>The  purpose of the hash function is to compute an index from a key.  That  index is then used to store and retrieve data from the array.  Many  higher-level languages include some type of hash table and hash  function.</p>
<p><img src="https://docs.google.com/drawings/pub?id=1dkczuc56r6R_LJT40BVolTouZz_L_UxOID_YsJVcpYk&amp;w=444&amp;h=260" alt="" /></p>
<p>As  you can see from the diagram above the hash(“string”) function  transforms a <a href="http://nickzarr.com/blog4/2011/02/20/what-in-the-hell-are-strings/">string</a> into an integer which is then used as the index into  the array.  This index is used to store or retrieve data in the array.</p>
<p>The  amount of time it takes to set or retrieve a value from a hash table is  the time it takes to run the hash function plus the time it takes to  access an element of the array.  For small data sets this could take  longer than simply iterating over each element of the array.  However,  since it is a fixed cost, for larger data sets using a hash table could  be much faster.</p>
<p>Assuming  we have a function <strong>set</strong> that takes a key to be hashed as its first  argument and a value to be stored as its second argument, we can create  the following hash table.</p>
<p><img src="https://docs.google.com/drawings/pub?id=1i0WdDGXT4Vuqh0RjzosQiAQwcXsy1GmR47O8WBu2svg&amp;w=444&amp;h=260" alt="" /></p>
<p>As  you can see from the above diagram we store both the key and the value  in the table.  The reason for this is collisions, which will be  explained below.</p>
<p>&nbsp;</p>
<h2>Collisions</h2>
<p>A  collision occurs when two different keys hash to the same value.   Collisions will occur in even the best designed hash tables, however  there are a few reasons why they can occur more frequently.  The first  reason is a poor hash function that does not distribute the indices  evenly throughout the table, this will be explained in more detail  below.  The second reason is that the array is too small.  If you have a  10 element array and you are trying to store 100 key/values in the hash  table, there will obviously be a lot of collisions.</p>
<p><img src="https://docs.google.com/drawings/pub?id=1HA9GeeT-Tkd0FfYm-FEA6qfJJ78Wn6uQ25zHJ6eC5BU&amp;w=444&amp;h=260" alt="" /></p>
<p>In  the diagram above “Puff” hashed to the same value as “Peter” causing a  collision.  Both “Peter” and “Puff” are now stored in the same index of  the array.  A certain amount of collisions are unavoidable in a hash  table, so we’ll need a way to deal with them.</p>
<p>The simplest way to deal with collisions is to make each element of the array a <a href="http://nickzarr.com/blog4/2011/02/20/what-in-the-hell-are-linked-lists/">linked list</a>.   Then, when a collision occurs, we simply search through the linked  list to find the data we want.  This way the hash function gives us a  ‘starting point’ to begin our search.</p>
<p>With  a good hash function and an appropriately sized array collisions are a  rare occurrence.  However it is possible, though very unlikely, that all  the key/values will be stored in one index and we will end up doing a  <a href="http://nickzarr.com/blog4/2011/02/24/what-in-the-hell-is-big-o/#linear">search on a linked list</a>.</p>
<p>&nbsp;</p>
<h2>What is a good hash function?</h2>
<p>The sole job of the hash function is to return a unique array index for each key.  This is not always possible and collisions can be expected to occur when the number of elements in the hash table approaches the square root of the size of the hash table&#8217;s array.  A hash function that does not provide a uniform distribution of keys will cause a lot of collisions, possibly breaking our hash table&#8217;s performance down from <a href="http://nickzarr.com/blog4/2011/02/24/what-in-the-hell-is-big-o/#constant">O(1)</a> to <a href="http://nickzarr.com/blog4/2011/02/24/what-in-the-hell-is-big-o/#linear">O(n)</a>.  The only workaround to an ineffective hash function is to make the hash table&#8217;s array much larger than the number of elements that it holds, which obviously wastes space.</p>
<p>&nbsp;</p>
<h2>Hash Table Implementations</h2>
<p><strong>Python</strong></p>
<p>Python&#8217;s equivalent of a hash table is called a dictionary or dict.</p>
<pre class="brush: python; title: ; notranslate">
# Creating a dictionary
d = {'a':5, 'b':10}

# Adding key/value pairs
d['c'] = 15

# Get value by key
d['c']
#===&gt; 15
</pre>
<p><strong>Chicken Scheme</strong></p>
<pre class="brush: plain; title: ; notranslate">
;; Creating a hash-table from an alist
(define d (alist-&gt;hash-table '((a . 5) (b . 10))))

;; Adding key/value pairs
(hash-table-set d 'c 15)

;; Get value by key
(hash-table-ref d 'c)
;===&gt; 15
</pre>
<p>&nbsp;</p>
<h2>Performance</h2>
<p>Indexing: <a href="http://nickzarr.com/blog4/2011/02/24/what-in-the-hell-is-big-o/#constant">O(1)</a><br />
Insertion/Removal: <a href="http://nickzarr.com/blog4/2011/02/24/what-in-the-hell-is-big-o/#constant">O(1)</a><br />
Search: <a href="http://nickzarr.com/blog4/2011/02/24/what-in-the-hell-is-big-o/#constant">O(1)</a></p>
<p>The performance listed above is what is normally found in specific hash table implementations.  There are actually a number of different implementations, each with their own advantages, disadvantages and performance guarantees.  Specific implementations will have to be presented in future WITH articles.</p>
<p>&nbsp;</p>
<h2>Links</h2>
<p><a title="What In The Hell Series" href="http://nickzarr.com/blog4/what-in-the-hell-series/">GOTO Table of Contents</a></p>
]]></content:encoded>
			<wfw:commentRss>http://nickzarr.com/blog4/2011/02/what-in-the-hell-are-hash-tables/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What In The Hell Are Strings</title>
		<link>http://nickzarr.com/blog4/2011/02/what-in-the-hell-are-strings/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-in-the-hell-are-strings</link>
		<comments>http://nickzarr.com/blog4/2011/02/what-in-the-hell-are-strings/#comments</comments>
		<pubDate>Sun, 20 Feb 2011 06:08:05 +0000</pubDate>
		<dc:creator>Nick Zarczynski</dc:creator>
				<category><![CDATA[What In The Hell Series]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[collections]]></category>
		<category><![CDATA[comp sci]]></category>
		<category><![CDATA[compsci]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[data structure]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[data type]]></category>
		<category><![CDATA[data types]]></category>
		<category><![CDATA[datatype]]></category>
		<category><![CDATA[datatypes]]></category>
		<category><![CDATA[Learn To Program]]></category>
		<category><![CDATA[learning programming]]></category>
		<category><![CDATA[programming basics]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Scheme]]></category>
		<category><![CDATA[sequences]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[What In The Hell]]></category>

		<guid isPermaLink="false">http://nickzarr.com/blog4/?p=126</guid>
		<description><![CDATA[GOTO All What In The Hell Articles Strings are a data type that consist of a sequence of characters. The simplest way to think about strings is that they are text. Strings are usually formed by &#8220;putting quotes around some text&#8221; or by using a string function. Strings come in two varieties. Terminated strings use [...]]]></description>
			<content:encoded><![CDATA[<p><a title="What In The Hell Series" href="http://nickzarr.com/blog4/what-in-the-hell-series/">GOTO All What In The Hell Articles</a></p>
<p>Strings are a data type that consist of a sequence of characters.   The simplest way to think about strings is that they are text. Strings  are usually formed by &#8220;putting quotes around some text&#8221; or by using a string function.</p>
<p>Strings come in two varieties.  Terminated strings use a  special character to signal the end of the string.  This character is  not allowed to appear in the string, as it would cause the string to  end, leaving out the rest.  Strings can also be implemented using a length field.  These strings do not need a terminating character as the  length of the string is stored with the string.  In many languages you  will not be able to tell which type of string you are using and it is  usually irrelevant.  However some languages, like C, require you to  place the termination character manually.</p>
<p>Strings can use a  variety of encodings for the characters contained within.  The two most  common are ASCII, which can represent 128 different characters, and  Unicode, which has support for over 100,000 different characters and  allows strings to hold non-English characters.  Both forms of encoding  are popular today, however most applications are moving toward Unicode  characters as the need for international software grows.</p>
<p>One  common problem with strings is representing characters that would  otherwise be interpreted by the language to mean something else.  For  instance the string &#8220;Have you read the book &#8220;1984&#8243; by George Orwell?&#8221;  would be interpreted as 3 expressions; (1) The string: &#8220;Have you read  the book &#8221; (2) The number: 1984 (3) The string: &#8221; by George Orwell?&#8221;.   This is obviously not what we want.  In this case we would use an escape  character to stop the quotes around &#8220;1984&#8243; from terminating the string.   Our new string would look something like this (this may vary from  language to language):  &#8220;Have you read the book &#8220;1984&#8243; by George  Orwell?&#8221;.  In this string the &#8221; character tells the language not to  end the string when it sees the following &#8220;.</p>
<p>&nbsp;</p>
<h2 id="terminology">Terminology for Strings</h2>
<p><strong>Concatenation:</strong><br />
Joining two strings together to form one string is called concatenation.</p>
<p><strong>Substring:</strong><br />
A  substring is a part of a larger string.  For example, in the string  &#8220;Hello World&#8221; one possible substring would be &#8220;Hello&#8221;, and another could  be &#8220;llo Wo&#8221;.</p>
<p><strong>Literal:</strong><br />
A literal string is a string that appears directly in the source code.  Strings formed by &#8220;putting quotes around them&#8221; are usually literal strings in a language.  To build a regular string you must usually use a string function.  In languages with mutable strings it is usually not possible to mutate a literal string, even where it is possible it is almost never a good idea.  More <a id="x.j7" title="here" href="http://en.wikipedia.org/wiki/String_literal">information on string literals can be  found here</a>.</p>
<p>&nbsp;</p>
<h2 id="code">Code</h2>
<p><strong>C</strong><br />
Strings in C are  represented as a null-terminated array of characters.  To create a  string you create an array, fill it with characters and place a null character directly after the last character in the string.  If you are  using string literals to create a string you do not have to supply the size or the null character, the compiler will do that for you.</p>
<pre class="brush: cpp; title: ; notranslate">
/* Create a string */
char s[6] = {'H', 'e', 'l', 'l', 'o', '&#92;&#48;'}
char sl[] = &quot;Hello World&quot;

/* Escape Character */
char e[50] = &quot;Have you read the book \&quot;1984\&quot; by George Orwell?&quot;
</pre>
<p><strong>Python</strong><br />
Strings  in Python are enclosed in quotes.  You can use one of three different  quoting styles to create a string.  Single quotes are useful when you  have double quote characters in the string.  Triple quotes allow you to  place newlines in the string.</p>
<pre class="brush: python; title: ; notranslate">
# Create a string
a = &quot;Hello World&quot;
b = 'Have you read &quot;1984&quot; by George Orwell?'
c = &quot;&quot;&quot;Materials:
Pen
Paper&quot;&quot;&quot;
# Escape Character
d = &quot;Have you read the book \&quot;1984\&quot; by George Orwell?&quot;
</pre>
<p><strong>Scheme</strong><br />
Strings in Scheme are enclosed in quotes or created with the &#8216;string&#8217; function.  Newlines may be embedded in any string.</p>
<pre class="brush: plain; title: ; notranslate">
;; Create a string
(define a &quot;Hello World&quot;)
(define b (string #H #e #l #l #o))

;; Escape Character:
(define c &quot;Have you read the book \&quot;1984\&quot; by George Orwell?&quot;)
</pre>
<p>&nbsp;</p>
<h2 id="performance">Performance</h2>
<p>The performance characteristics of a string will depend on the data type that is used to represent them.  Most often <a title="What In The Hell Are Arrays" href="http://nickzarr.com/blog4/2011/02/20/what-in-the-hell-are-arrays/">arrays</a> are used, however <a title="What In The Hell Are Linked Lists" href="http://nickzarr.com/blog4/2011/02/20/what-in-the-hell-are-linked-lists/">linked lists</a> are somewhat common as well.  Strings may also be either mutable or immutable which can have an impact on their algorithmic performance.</p>
<p>Because of these factors an accurate overview of the performance of strings cannot be given.  However in the vast majority of cases strings will share the same characteristics as <a title="What In The Hell Are Arrays" href="http://nickzarr.com/blog4/2011/02/20/what-in-the-hell-are-arrays/#performance">arrays</a>.</p>
<p>In the case of terminated strings implemented as an array the indexing time can still be O(1) however supplying an index greater than the length will access memory outside the string and, hopefully, cause an error.  You may need to determine the length first to prevent this, which would be an O(n) operation.</p>
<p>&nbsp;</p>
<h2 id="links">Links</h2>
<p><a href="http://www.sidhe.org/~dan/blog/archives/000255.html">What the heck is: a String</a><br />
A great article on strings, much more in-depth than this one.  Incidentally where I got the inspiration for this series. </p>
<p>&nbsp;</p>
<p><a title="What In The Hell Series" href="http://nickzarr.com/blog4/what-in-the-hell-series/">GOTO Table of Contents</a></p>
]]></content:encoded>
			<wfw:commentRss>http://nickzarr.com/blog4/2011/02/what-in-the-hell-are-strings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What In The Hell Are Linked Lists</title>
		<link>http://nickzarr.com/blog4/2011/02/what-in-the-hell-are-linked-lists/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-in-the-hell-are-linked-lists</link>
		<comments>http://nickzarr.com/blog4/2011/02/what-in-the-hell-are-linked-lists/#comments</comments>
		<pubDate>Sun, 20 Feb 2011 05:41:00 +0000</pubDate>
		<dc:creator>Nick Zarczynski</dc:creator>
				<category><![CDATA[What In The Hell Series]]></category>
		<category><![CDATA[collections]]></category>
		<category><![CDATA[comp sci]]></category>
		<category><![CDATA[compsci]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[data structure]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[data type]]></category>
		<category><![CDATA[data types]]></category>
		<category><![CDATA[datatype]]></category>
		<category><![CDATA[datatypes]]></category>
		<category><![CDATA[Learn To Program]]></category>
		<category><![CDATA[learning programming]]></category>
		<category><![CDATA[programming basics]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Scheme]]></category>
		<category><![CDATA[sequences]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[What In The Hell]]></category>

		<guid isPermaLink="false">http://nickzarr.com/blog4/?p=114</guid>
		<description><![CDATA[GOTO All What In The Hell Articles This is the first in a series of articles describing programming concepts. This series assumes you have some basic experience in programming, but attempts to describe these concepts as simplistically as possible. This series will mainly use Python as the language of choice for examples. This is not [...]]]></description>
			<content:encoded><![CDATA[<p><a title="What In The Hell Series" href="http://nickzarr.com/blog4/what-in-the-hell-series/">GOTO All What In The Hell Articles</a></p>
<p>This is the first in a series of  articles describing programming concepts.  This series assumes you have  some basic experience in programming, but attempts to describe these  concepts as simplistically as possible.  This series will mainly use  Python as the language of choice for examples.  This is not because of  any perceived superiority of Python over other languages, rather Python  is basically executable psudo-code so we can express the concepts simply  and give you a chance to play with them inside an interpreter.</p>
<p>&nbsp;</p>
<h2>Singly-Linked Lists</h2>
<p>A  linked list is one of the simplest data structures, at its heart it is  simply a two element node (or cell).  The first slot contains the data  and the second slot contains a pointer to the next node.  A simple  illustration demonstrates this concept more concretely&#8230;</p>
<p><img src="https://docs.google.com/drawings/pub?id=1D5UHl9anYnTkiFgjCKYt14-MTUnCa4oXYiqGQkL6NpM&amp;w=707&amp;h=110" alt="" /></p>
<p>The graphic above represents the list (1, 2, 3).  Each node in the list  has two parts, data and pointer.  The last node has a special symbol  &#8216;Nil&#8217; to tell both us and the computer that we have reached the end of  the list.  The python code for each node in the list is below&#8230;</p>
<pre class="brush: python; title: ; notranslate">

class Node:

  def __init__(self, data=None, next=None):
    self.data = data
    self.next  = next

  def __repr__(self):
    return repr(self.data)
</pre>
<p>To make a list out of these nodes we would need to make a node for each element in the list (1, 2 and 3) and link them together.</p>
<pre class="brush: python; title: ; notranslate">
a = Node(1, Node(2, Node(3, None)))
print a
#===&gt; 1
print a.next
#===&gt; 2
print a.next.next
#===&gt; 3
print a.next.next.next
#===&gt; None
</pre>
<p>Notice the nesting of the calls to Node above.  Linked lists are an  inherently nested data structure.  To construct the list without having  to nest the calls to Node we would have to work backwards.  We have to  do this because if we try to create the first node (1) there is not  currently a second node to which we can point to.</p>
<pre class="brush: python; title: ; notranslate">
c = Node(3, None)
b = Node(2, c)
a = Node(1, b)
</pre>
<p>The graphic below shows another way to look at Nodes that reflects their nested nature.</p>
<p><img src="https://docs.google.com/drawings/pub?id=1Txg8YSrmMw1JUwel7C7xQFKBFlDn86NBdWHSVd6SaGU&amp;w=707&amp;h=361" alt="" /></p>
<p>Programming languages like Lisp and Scheme make heavy use of linked  lists.  Consequently they have a rich library of functions for dealing  with them.  The code below shows how you create a linked list in Scheme  using the &#8216;cons&#8217; function.  &#8216;cons&#8217; is short for construct and the nodes  are called &#8216;cons cells&#8217;.</p>
<pre class="brush: plain; title: ; notranslate">

(define a (cons 1 (cons 2 (cons 3 '()))))
a
;===&gt; (1 2 3)
</pre>
<p>Now that we have created a list with cons (note its similarity to the  Python example of Node) we can use the built-in procedures car and cdr  to access elements of that list.</p>
<pre class="brush: plain; title: ; notranslate">

(car a)
;===&gt; 1
(cdr a)
;===&gt; (2 3)
(car (cdr a))
;===&gt; 2
(cdr (cdr a))
;===&gt; (3)
(car (cdr (cdr a)))
;===&gt; 3
(cdr (cdr (cdr a)))
;===&gt; ()
</pre>
<p>Most Scheme implementations usually define shorthand functions for  performing these operations, like cadr and caddr (and many more).  Many  also provide more descriptive names such as first, rest, second,  third&#8230;</p>
<pre class="brush: plain; title: ; notranslate">

(cadr a)
;===&gt; 2
(caddr a)
;===&gt; 3
(cddr a)
;===&gt; (3)
(first a)
;===&gt; 1
(rest a)
;===&gt; (2 3)
(second a)
;===&gt; 2
(third a)
;===&gt; 3
</pre>
<p>Constructing linked lists and accessing their elements is usually most  elegantly accomplished using recursion.  The Scheme code below shows how  we would recursively construct a list of integers, we have to do it  backward to avoid a call to (reverse).</p>
<pre class="brush: plain; title: ; notranslate">
(define (make-list-of-integers start stop)
  (let loop ((s stop) (aList '()))
    (if (&lt; s start)
        aList
        (loop (- s 1) (cons s aList)))))

(make-list-of-integers 0 5)
;===&gt; (0 1 2 3 4 5)
</pre>
<p>&nbsp;</p>
<h2>Circular Linked Lists</h2>
<p>A circular list is a linked-list  where the last element&#8217;s pointer is set to the first element of the  list.  The graphic below shows the nodes of a circular list.</p>
<p><img src="https://docs.google.com/drawings/pub?id=1BHo7jSm6weN9hkNgtSKsSwqo8x4VingCrEow_AZBWYQ&amp;w=733&amp;h=215" alt="" /></p>
<p>Using the Node class that we defined above we can create a circular list in Python as follows&#8230;</p>
<pre class="brush: python; title: ; notranslate">

a = Node(1)
a.next = Node(2, Node(3, a))
a.next
#===&gt; 2
a.next.next
#===&gt; 3
a.next.next.next
#===&gt; 1
a.next.next.next.next
#===&gt; 2
</pre>
<p>The Scheme code for defining a circular list is below&#8230;</p>
<pre class="brush: plain; title: ; notranslate">
(define a (cons 1 (cons 2 (cons 3 '()))))
(set-cdr! (cddr a) (car a))
a
;===&gt; (1 2 3 . 1)
</pre>
<p>&nbsp;</p>
<h2>Doubly-Linked Lists</h2>
<p>Usually the term linked list refers to  singly-linked lists as we have implemented them above.  However there  are other ways of implementing linked lists as well.  The next most  common form of linked list is the doubly-linked list.  This is very  similar to singly-linked lists except that each node in the list also  contains a pointer to the previous node as shown in the graphic below.</p>
<p><img src="https://docs.google.com/drawings/pub?id=1qgi_rQ3bJU9h_fIsJ-8vxBcG3nYPzTsjmfyWMp5US6I&amp;w=934&amp;h=124" alt="" /></p>
<p>Below is the Python code to implement doubly-linked lists.</p>
<pre class="brush: python; title: ; notranslate">
class dNode:
  def __init__(self, prev=None, data=None, next=None):
    self.prev = prev
    self.data = data
    self.next = next

  def __repr__(self):
    return str(self.data)

a = dNode(data=1)
b = dNode(data=2)
c = dNode(data=3)
d = dNode(data=4)
a.next = b
b.prev, b.next = a, c
c.prev, c.next = b, d
d.prev = c

a
#===&gt; 1
a.next
#===&gt; 2
a.next.next
#===&gt; 3
a.next.next.prev
#===&gt; 2
a.next.next.next.prev
#===&gt; 3
</pre>
<p>To make the doubly-linked list easier to use we would have to add some  helper functions.  As you can see it is fairly difficult to create a  doubly-linked list, so we&#8217;ll define an append function here that will  make creating one a little easier.</p>
<pre class="brush: python; title: ; notranslate">
def dAppend(dLinkedList, nodeToAppend):
  if dLinkedList.next == None:
    dLinkedList.next = nodeToAppend
    nodeToAppend.prev = dLinkedList
  else:
    dAppend(dLinkedList.next, nodeToAppend)

a = dNode(data=1)
dAppend(a, dNode(data=2)
dAppend(a, dNode(data=3)
a
#===&gt; 1
a.next
#===&gt; 2
a.next.next
#===&gt; 3
a.next.next.prev
#===&gt; 2
a.next.prev
#===&gt; 1
</pre>
<p>The simplest way to achieve this is through a recursive function as  defined above.  However Python does not perform tail-call optimization  and will either run out of memory or reach its recursion limit (default  is 1000) on large lists.  Below is a non-recursive version of the same  function.</p>
<pre class="brush: python; title: ; notranslate">
def dNodeAppend(dLinkedList, nodeToAppend):
  node = dLinkedList
  while True:
    if node.next == None:
      node.next = nodeToAppend
      nodeToAppend.prev = node
      break
    else:
      node = node.next

a = dNode(data=1)
dNodeAppend(a, dNode(data=2))
dNodeAppend(a, dNode(data=3))
dNodeAppend(a, dNode(data=4))
a
#===&gt; 1
a.next
#===&gt; 2
a.next.next
#===&gt; 3
a.next.next.next
#===&gt; 4
a.next.next.next.prev
#===&gt; 3
a.next.next.prev
#===&gt; 2
a.next.prev
#===&gt; 1
</pre>
<p>&nbsp;</p>
<h2>Performance</h2>
<p>Insertion/Removal at head: O(1)<br />
Insertion/Removal at n: O(n)<br />
Indexing: O(n)<br />
Search (unsorted): O(n)</p>
<h2>Links</h2>
<p>How to Think Like A Computer Scientist &#8211; Chapter 17 &#8211; Linked Lists<br />
This chapter describes a way of implementing linked lists in Python.  It also covers circular lists.<br />
<a href="http://greenteapress.com/thinkpython/html/chap17.html">http://greenteapress.com/thinkpython/html/chap17.html</a></p>
<p>An Introduction to Scheme and its Implementation &#8211; Lists<br />
<a href="http://www-pu.informatik.uni-tuebingen.de/users/sperber/pfg-2001/scheme/schintro-v14/schintro_93.html">http://www-pu.informatik.uni-tuebingen.de/users/sperber/pfg-2001/scheme/schintro-v14/schintro_93.html</a></p>
<p>Wikipedia Article on Linked Lists<br />
<a href="http://en.wikipedia.org/wiki/Linked_list">http://en.wikipedia.org/wiki/Linked_list</a></p>
<p><a title="What In The Hell Series" href="http://nickzarr.com/blog4/what-in-the-hell-series/">GOTO Table of Contents</a></p>
]]></content:encoded>
			<wfw:commentRss>http://nickzarr.com/blog4/2011/02/what-in-the-hell-are-linked-lists/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What In The Hell Are Arrays</title>
		<link>http://nickzarr.com/blog4/2011/02/what-in-the-hell-are-arrays/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-in-the-hell-are-arrays</link>
		<comments>http://nickzarr.com/blog4/2011/02/what-in-the-hell-are-arrays/#comments</comments>
		<pubDate>Sun, 20 Feb 2011 04:45:31 +0000</pubDate>
		<dc:creator>Nick Zarczynski</dc:creator>
				<category><![CDATA[What In The Hell Series]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[collections]]></category>
		<category><![CDATA[comp sci]]></category>
		<category><![CDATA[compsci]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[data structure]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[data type]]></category>
		<category><![CDATA[data types]]></category>
		<category><![CDATA[datatype]]></category>
		<category><![CDATA[datatypes]]></category>
		<category><![CDATA[Learn To Program]]></category>
		<category><![CDATA[learning programming]]></category>
		<category><![CDATA[programming basics]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Scheme]]></category>
		<category><![CDATA[sequences]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[What In The Hell]]></category>

		<guid isPermaLink="false">http://nickzarr.com/blog4/?p=104</guid>
		<description><![CDATA[GOTO All What In The Hell Articles Arrays are one of the two fundamental data structures in computer science (the other is the tree).  An array is a contiguous (uninterrupted or sequential) block of memory that stores a set of elements. Elements in an array can be accessed by their index.  An array’s index usually [...]]]></description>
			<content:encoded><![CDATA[<p><a title="What In The Hell Series" href="http://nickzarr.com/what-in-the-hell-series/">GOTO All What In The Hell Articles</a></p>
<p>Arrays  are one of the two fundamental data structures in computer science (the other is the tree).  An array is a contiguous (uninterrupted or sequential) block of memory that stores a set of elements.</p>
<p>Elements in an array can be accessed by their index.  An array’s  index usually starts at 0 and stops at the number of elements in the  array minus 1.  The diagram below shows an array of the characters in  the string “HELLO”.</p>
<p><img src="https://docs.google.com/drawings/pub?id=1h5j8-0wspb8Tdb8lIcd97Q56JRNKpYla3KC0ofl_Jfw&amp;w=240&amp;h=97" alt="" /></p>
<p>In the array above the element at index 0 is the character “H”, the element at index 1 is the character “E” and so on.</p>
<p>Arrays have <a href="http://nickzarr.com/blog4/2011/02/24/what-in-the-hell-is-big-o/#constant">constant time access</a> to the elements stored within.  This means  that it takes the same amount of time to access any element.  For  example, it would take the same amount of time to access the last  element of an array as it would the first, or even an element in the  middle.</p>
<p><img src="https://docs.google.com/drawings/pub?id=1m6qrVhuVfZOh2wAB9Z0LvqlKvAwIPi9LKYFl3TxTbbg&amp;w=450&amp;h=200" alt="" /></p>
<p>A  multi-dimensional array is simply an array of arrays.  A spreadsheet is  a form of multi-dimensional array.  The diagram above shows a 3 element  array where each element is also a 3 element array.  Multi-dimensional arrays can be used for a variety of tasks from tracking pieces on a chess board to drawing graphics on the screen.  </p>
<p>A &#8220;true&#8221; multi-dimensional array is implemented as a contiguous block of memory, just like the regular array.  However it is also common practice to implement multi-dimensional arrays as <a href="http://nickzarr.com/blog4/2011/02/21/what-in-the-hell-are-trees/">trees</a>.</p>
<p>&nbsp;</p>
<h2 id="code">Code</h2>
<p><strong>C</strong></p>
<p>An array in C can contain elements of only one type/size and is not resizable.</p>
<pre class="brush: cpp; title: ; notranslate">
/* Create an array */
int a[3] = {5, 10, 15};

/* Access an array element */
a[1] /*==&gt; 10 */
</pre>
<p><strong>Python</strong></p>
<p>An array in Python is called a list.  It can contain elements of any type/size and is resizable.</p>
<pre class="brush: python; title: ; notranslate">

# Create an array
a = [5, 10, 15]

# Access an array element

a[1]  #==&gt;  10
</pre>
<p><strong>Scheme</strong></p>
<p>An array in Scheme is called a vector.  It can contain elements of any type/size and may be resizable.</p>
<pre class="brush: plain; title: ; notranslate">

;; Create an array

(define a (vector 5 10 15))
(define b #(5 10 15))

;; Access an array element

(vector-ref a 1)  ;;==&gt;  10
</pre>
<p>&nbsp;</p>
<h2 id="index">What is the index?</h2>
<p>An index is a way of labeling elements in an array.  The first element is usually located at index 0, the second at index 1, the third at 2 and so on.  The index is used to calculate the address of the element.</p>
<p>To understand this we have to remember that an array is just a reserved block of memory.  Let&#8217;s look at a simple diagram of how the memory in your computer is organized.</p>
<p><img src="https://docs.google.com/drawings/pub?id=1hqNy6rSjGddZAaiY4SOBzBkM6s-T7wVoVXNlwWCMxqo&amp;w=624&amp;h=229" alt="" /></p>
<p>Each block of memory can hold one value (usually equal to an integer in a language like C).  To create an array we reserve a block of memory big enough to hold all our elements.  If we had 4 elements it would look like the diagram below.</p>
<p><img src="https://docs.google.com/drawings/pub?id=1YSrHN-Ex7tuHh3PsLf-qewfV7fEW970lT9yvrUNS_ck&amp;w=617&amp;h=336" alt="" /></p>
<p>To access our array we only need to keep around the address of the beginning of our reserved space or 102 in this example.  This address is equal to the index 0.  But our index values still don&#8217;t point us at the right addresses yet.  To do that we multiply the index by the size of an element then add that value to the start of our array.  Let&#8217;s see how that maps out on our array.</p>
<p><img src="https://docs.google.com/drawings/pub?id=135eYsmvUgs3zDFtaWo4rmZLO9PrBQugp2L8EiaG7sEc&amp;w=585&amp;h=229" alt="" /></p>
<p>Sometimes elements in an array are larger than just one block of memory.  Here is what an array looks like when each element is two blocks of memory large.</p>
<p><img src="https://docs.google.com/drawings/pub?id=1lb6hlyzcmPgyEKN-N20pYK57PzWxbKDO_e_BCLeC6Xg&amp;w=585&amp;h=229" alt="" /></p>
<p>An array can only contain elements of one size.  This is because we need to multiply the index by the size of the elements in the array.  How then do some languages let you put elements in an array that are different sizes?  One simple way to do that is to use the size of the largest element in the array for indexing.  This leads to some wasted space when storing smaller elements but allows the array to work just like before.</p>
<p>How do we know when we&#8217;re at the end of the array?  One way is to store the length of the array with the address to the first element.  In that case we just check if the index is &lt;= the length.  Another way to signal the end of an array is a sentinel node.  A sentinel node is a value that can only occur at the end of an array and signals the end.</p>
<p>&nbsp;</p>
<h2 id="performance">Performance</h2>
<p><a title="What In The Hell Is Big O" href="http://nickzarr.com/blog4/2011/02/24/what-in-the-hell-is-big-o#constant">Indexing: O(1)</a><br />
<a title="What In The Hell Is Big O" href="http://nickzarr.com/blog4/2011/02/24/what-in-the-hell-is-big-o/#linear">Insertion/Removal: O(n)</a><br />
<a title="What In The Hell Is Big O" href="http://nickzarr.com/blog4/2011/02/24/what-in-the-hell-is-big-o/#linear"> Search: O(n)</a></p>
<p>Arrays are either static or dynamic.  A static array is set at a fixed size, elements can be inserted and removed but the array cannot grow or shrink.  A dynamic array, on the other hand, can grow or shrink.  Dynamic arrays are often initialized to be larger than the set of elements it contains.  When enough elements are added to the array, such that the number of elements is greater than the array size, the array must be resized.  Resizing an array could be a costly operation if the array is large or the act of copying is slow.</p>
<p><img src="https://docs.google.com/drawings/pub?id=1xpn1Bl-aASdtN7_Pjxvv-Zm-aii9DJmqIZ17ih1n8k4&amp;w=607&amp;h=330" alt="" /></p>
<p>&nbsp;</p>
<h2 id="links">Links</h2>
<p><a title="What In The Hell Series" href="http://nickzarr.com/what-in-the-hell-series/">GOTO Table of Contents</a></p>
]]></content:encoded>
			<wfw:commentRss>http://nickzarr.com/blog4/2011/02/what-in-the-hell-are-arrays/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

