# C9 Lectures: Dr. Erik Meijer - Functional Programming Fundamentals Chapter 6 of 13

- Posted: Nov 05, 2009 at 7:53AM
- 65,554 views
- 25 comments

Loading user information from Channel 9

Something went wrong getting user information from Channel 9

Loading user information from MSDN

Something went wrong getting user information from MSDN

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

- Posted: Nov 05, 2009 at 7:53AM
- 65,554 views
- 25 comments

- To download, right click the file type you would like and pick “Save target as…” or “Save link as…”

- It's an easy way to save the videos you like locally.
- You can save the videos in order to watch them offline.
- If all you want is to hear the audio, you can download the MP3!

- If you want to view the video on your PC, Xbox or Media Center, download the High Quality MP4 file (this is the highest quality version we have available).
- If you'd like a lower bitrate version, to reduce the download time or cost, then choose the Medium Quality MP4 file.
- If you have a Windows Phone, iPhone, iPad, or Android device, choose the low or medium MP4 file.
- If you just want to hear the audio of the video, choose the MP3 file.

Right click “Save as…”

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 5

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.

## Follow the discussion

Oops, something didn't work.

## What does this mean?

Following an item on Channel 9 allows you to watch for new content and comments that you are interested in. You need to be signed in to Channel 9 to use this feature.## What does this mean?

Following an item on Channel 9 allows you to watch for new content and comments that you are interested in and view them all on your notifications pagesign up for email notifications?

Did Erik get sign-off on the poster-frame for chapter 6?

Any chances of Don Syme doing a similar training series on F#???

I'd love to learn me some F#

Yes, in fact. Stay tuned.

C

I'd use the word 'Brilliant', Charles, but for that I think we need to break out 'Awesome'!

I'm looking forward to that already!

The Don Syme lectures will be broken up into 3 parts. These won't air until after the PDC.

C

I think it's truly great that you do these, I know Don has already spoken several places about F# but a concentrated lecture series is really something which will be even more beneficial to it.

Allright, back to the lazy world

I couldn't resist the Zip homework first, it looks extremely simple, first with tuple-zip

Then we can verify that this behavior is correct by matching up with the built-in behavior

It's trivial to rewrite the tuple definition to the generic function definition.

Very nice show on recursive, even the topic is a basic programming skill.

Hugs is new for me. Sometimes I could not get examples in the show to work. Anyway, here are some interfactive examples I played during the time I watch the show.

I would like to see more codes related to the topics. Enjoy it!

It seems that the animation got lost in the full screen slides.

Anyway, if anyone gets stuck on implementing a more efficient reverse function, here's a hint:

accumulator.About the presented Haskell zip function, its definition can be shorter if you reorder the clauses:

Took a look at Bart's Zip blog post, it also mentions exceptions. How about an expression tree transformation that takes an exception-throwing iterator and rewrites it to force the exceptions first, defining a second helper iterator for the rest. Don't know if it's possible.

A less ambitious (and alas less desirable) approach is to implement a first-is-strict combinator that forces and memoizes the first element of the iterator, if there is one, thus forcing the initial exception into the light.

Nontheless, both of these approaches are inferior: they delay the inevitable: that some nested throws clause will pop up in the middle of an iterator. This sucks for exceptions as "control flow" rather than proper return types you can examine using regular if statements rather than try/catch.

(Not appropos: see this video (from LtU post): - is the world of tomorrow a flashback to the past? Beautiful unicode in APL - when will this come to mainstream; waiting since the sixties seems cruel and inhuman to me! The video also contains a mention of funny puns on the APL name, like APL π, pronounced "apple pie"; and a quote: "changes of a fundamental kind take a long time" - I'll buy that for a dollar)

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.

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...

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

Here is my take on Append:

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:

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)

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.!

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

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 theiterator?)disposedcorrectly under all circumstances. (Tip:don't overcomplicateit; 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!

(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.

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

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

OK, this is a quick write-up that would seem to fit the bill

I think it's amusing that you refer to Hughes paper on FP as such a good paper, because I'm taking a class of his right now, and he strongly recommends this video series And, yes, it's a great paper. Good read. Thanks for the awesome video lectures, they complement his course very well!

Here is my take on the homework:

1.- Define:

and :

concat:

replicate:

Select the Nth statement of a list:

elem:

2. Define merge :: [Int] -> [Int] -> [Int]

3. Define merge sort:

I'm having a blast with this series.

It's probably not a complete coincidence that Charles speaks of the high priests of the Lambda calculus.

- folded hands, closed eyes, tilted head, pointing to the sky

May the monad be with you!

Indeed! You are right, brothers. That still shot captures a moment of extreme reverence, a moment in time where Dr. Meijer, a reverend of the Lambda, evangelist of the functional composition of the monadic universe, exudes his deep thanks and praises for the higher-order function in the sky.

C

Somewhere closer to the end of the talk, Mr. Meijer says that segregation of common parts

of a function from the distinct parts is only possible with lazy evaluation (with the two clouds

drawn on the board). Didn't he really mean functional composition instead, which is more

general than lazy evaluation? If the distinct parts have no side-effects and the common part has

no influence on the distinct parts besides parametrization (as is the case with pure functions),

then you can also call the common part as a function with distinct parts as pure functional or

value parameters to that common function.

Of course you might save some computations with lazy evaluation, but I am quite sure I have

applied this technique with eager evaluation as well!

Am I missing something?

Finally got some time and went through this lecture too...

(!!) :: [a] -> Int -> a

(a:as) !! 0 = a

(a:as) !! (n + 1) = as Main.!! n

(++) :: [a] -> [a] -> [a]

[] ++ [] = []

as ++ [] = as

[] ++ bs = bs

(a:as) ++ (bs) = a : (Main.++) as bs

(&&) :: Bool -> Bool -> Bool

True && True = True

_ && _ = False

length :: Num a => [a] -> Int

length [] = 0

length [x] = 1

length (a:as) = 1 + Main.length as

take :: Num a => Int -> [a] -> [a]

take 0 _ = []

take _ [] = []

take (n + 1) (a:as) = a : Main.take n as

drop :: Num a => Int -> [a] -> [a]

drop 0 as = as

drop _ [] = []

drop (n + 1) (a:as) = Main.drop n as

qsort :: Ord a => [a] -> [a]

qsort [] = []

qsort [a] = [a]

qsort (a:as) = qsort smaller Main.++ [a] Main.++ qsort greater

where

smaller = [x | x <- as, x <= a]

greater = [x | x <- as, a < x]

and :: [Bool] -> Bool

and [] = True

and (a:as) = a Main.&& Main.and as

concat :: [[a]] -> [a]

concat [] = []

concat (a:as) = a Main.++ Main.concat as

replicate :: Int -> a -> [a]

replicate 0 a = []

replicate (n + 1) a = a : Main.replicate n a

elem :: Eq a => [a] -> a -> Bool

elem [] _ = False

elem (a:as) s = if a == s then True else Main.elem as s

merge :: [Int] -> [Int] -> [Int]

merge [] [] = []

merge [] bs = bs

merge as [] = as

merge as bs = qsort $ as Main.++ bs

msort :: [Int] -> [Int]

msort [] = []

msort [a] = [a]

msort as = merge (msort $ Main.take n as) (msort $ Main.drop n as)

where

n = Main.length as `div` 2

Sohail Qayum Malik.

## Remove this comment

## Remove this thread

Close