Posted By: Charles | Nov 5th @ 7:53 AM | 33,774 Views | 24 Comments
We've kicked off C9 Lectures with a journey into the world of Functional Programming with functional language purist and high priest of the lambda calculus, Dr. Erik Meijer (you can thank Erik for many of the functional constructs that have shown up in languages like C# and VB.NET. When you use LINQ, thank Erik in addition to Anders). 

We will release a new chapter in this series every Thursday.

In Chapter 6, Dr. Meijer guides us through the world of recursive functions. In Haskell, functions can be defined in terms of themselves.  Such functions are called recursive.
For example: 

factorial 0 = 1
factorial (n+1) = (n+1) * factorial n

factorial maps 0 to 1, and any other positive integer to the product of itself and the factorial of its predecessor.

Some functions, such as factorial, are simpler to define in terms of other functions. As we shall see, however, many functions can naturally be defined in terms of themselves.

Properties of functions defined using recursion can be proved using the simple but powerful mathematical technique of induction.


You should watch these in sequence (or skip around depending on your curent level of knowledge in this domain):

Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5

Now, we do have a textbook and you should go buy it: The great Graham Hutton's Programming in Haskell. We worked with the publisher, Cambridge University Press, to get all Niners a 20% discount on the book. Now, you don't need the book to learn a great deal from this lecture series since Graham's website has all the slides and samples from the book as well as answers to the exercises. That said, it's highly recommended reading and you should consider it.

The promotion code is 09HASK and it is vaild on both the Hardback:

9780521871723 and Paperback: 9780521692694. The catalog pages are:

Hardback:

http://www.cambridge.org/us/catalogue/catalogue.asp?isbn=9780521871723 and the paperback is:

http://www.cambridge.org/us/catalogue/catalogue.asp?isbn=9780521692694

Note: This special offer is valid until December 31, 2009

Rating:
13
0
we
we

The factorial function defined with the (n+k) pattern will not go into an infinite loop when given a negative number. (n+k) patterns only match with numbers >=k. So you would rather get an exception about non-exhaustive patterns.

we
we

It's so cool to hear Erik saying that explicit recursion can be abstracted. I can't wait to see you mention the paper "Functional programming with bananas, lenses, envelopes and barbed wire"!

My book finally arrived, after just over a month of waiting... Tongue Out

Again, nice lecture.  I'm lloking forward to the rest.

 

Here is my take on Append:

public static IEnumerable<T> Append<T>(this IEnumerable<T> a, IEnumerable<T> b)
{
    foreach (var t in a)
    {
        yield return t;
    }
    foreach (var t in b)
    {
        yield return t;
    }
}

More homework:

1) Prove that fac n = recfac n

 

Definition of recfact n:

(1)        recfac 0 = 1

(2)        recfac (n+1) = (n+1) * recfact n

 

Definition of fac n:

(3)        fac n = product [1...n]

                        where

(4)                                product [] = 1

(5)                                product [1..n] = 1 * 2 * … (n-1) * n

 

We want to prove that:

Devil                    fact n = recfac n

 

Using induction:

 

Case n = 0

            fac 0 = recfac 0

 

            fac 0 = 1 (Eq. 1)

 

            recfac 0 = product [1 .. 0] = product [] = 1 (Eq. 4)

 

            1 = 1

 

Case (m + 1):

(7)        fac (m+1) = recfac (m+1)

 

fac (m+1) = product [  1..(m+1)] = 1 * 2 *….* m * (m+1)

 

  1. Using Eq.5:       1 * 2 *….* m  = product [1..m]

 

            fac (m+1) = 1 * 2 *….* m * (m+1)  =(product [1..m]) * (m+1)

 

(8)        fac (m+1) = (product [1..m]) * (m+1)

 

Using Eq 2 :  recfac (m+1) = (m+1) * recfac m

 

 

Replacing Eq 8 into Eq 7:

            fac (m+1) = (product [1..m]) * (m+1) = recfac (m+1)

 

Using Eq 2 on the right side:

            (product [1..m]) * (m+1) = (m+1) * recfac m

 

Dividing by (m+1) on both sides:

            product [1..m] = recfac m

 

Using Eq 3 on the left side:

            fac m = recfac m

 

Q.E.D.! Smiley

bdesmet
bdesmet
Bart De Smet

Concerning the Zip homework, a couple of things to keep in mind:

  • Do proper argument validation: both input IEnumerable objects and the zipper function should not be null. This is a bit tricky. (Tip: when does the exception get thrown by the iterator?)
  • IEnumerator objects implement IDisposable. Make sure the enumerator objects get disposed correctly under all circumstances. (Tip: don't overcomplicate it; the language can do lots of work for you)

Additional brain gymnastics can be triggered by trying to define Zip in terms of other LINQ Standard Query Operators. Enjoy!

exoteric
exoteric
I : Next<I>

(I'm not at home right now so no thorough answer, but -) I know you need to force exception handling by using another method to do the actual implementation, otherwise the exceptions will be thrown when the stream is enumerated and for disposal, well we have using statements, need to check that later. Nice having you here, Bart.

 

Smiley

Towards the end of the video Erik referred to the slides. Where are those slides???

It's a hassle working on the homework off the video stream, would be much easier if looking thru the PPT.

Can we get the download link?

 

Thanks

we
we

http://www.cs.nott.ac.uk/~gmh/book.html#slides

Microsoft Communities