Lecture 21, CS 302-7 and 8, March 9

 

  1. Reminders
    1. Exam 1 – we’ll discuss next week, probably Wednesday

                                                               i.      Why?  Some people still haven’t taken it

    1. Program 2 – March 16

                                                               i.      10pm today-pairs must be registered

    1. Program 1 – coming back soon (early next week)
  1. Recursion
    1. Before we start, note – you will not be tested on this etc

                                                               i.      But it is good to know

Recursion:

                        Definition -  "Recursion".

    1. Concept – methods that call themselves
    2. One of the most powerful tools in programming

                                                               i.      Elegant/simple/concise solutions to complicated problems – sometimes (just about) the only solution

                                                             ii.      Also one of the most confusing

    1. Source of lots of bad CS humor…

                                                               i.      http://xkcd.com/244/

                                                             ii.      Search recursion in google

    1. Note – recursive programs will need at least two methods
    2. Vocab:

                                                               i.      Recursive case

1.      Case that repeats – occurs most of the time

2.      Calls itself

                                                             ii.      Base case

1.      Case that terminates – does not call itself

2.      Often something like – when length is 0, when value is 0, etc

    1. Lazy evaluation

                                                               i.      Don’t do something until you have to.

                                                             ii.      In Java – keep doing things on the right side of the equation as long as possible

    1. Example 1 – Factorial

                                                               i.      Factorials.java

    1. Visual examples:

                                                               i.      Note – not quite the same, but similar sort of idea.

                                                             ii.      Pictures containing themselves (like methods calling selves)

                                                            iii.      http://www.youtube.com/watch?v=QsMvoui5WlQ

1.      Sierpinski triangle – confined recursion of triangles

                                                           iv.      Droste effect

1.      http://en.wikipedia.org/wiki/File:Droste.jpg

2.      Picture contains itself

3.      http://www.youtube.com/watch?v=hKFhI9hdteE

a.       MC Escher

    1. Example 3 – Anagrams

                                                               i.      Definition – all possible orders of the letters in the word

                                                             ii.      Idea – pick a letter, stick it in front.  You have n-1 letters left.  Pick a letter from that, stick it in front.  Etc.  Do this for each possible selection.

                                                            iii.      Anagrams.java

  1. HW – read 6.6