Posted By: Charles | Oct 29th, 2009 @ 10:31 AM | 48,088 Views | 29 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 5, Dr. Meijer introduces and digs into List Comprehensions. In mathematics, comprehension notation is used to construct new sets from old sets. In Haskell, you can create new lists from old lists using a similar comprehension syntax:

[x^2 | x <- [1..5]]

The above notation represents the list [1,4,9,16,25] of all numbers x^2 such that x is an element of the list [1..5]. The <- [1..5] syntax is known as a generator and list comprehensions can have mulitple generators that can have explicit dependencies on other generators. You will also learn about guards, which restrict values created by earlier generators.

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

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:
18
0

At last Thursday is here. Smiley

 

Excelent presentation as always. The only thing is that you missed an excelent opportunity to implement "where" using a list comprehension.

 

where' :: [a] -> (Int -> a -> Bool) -> [a]
where' xs p = [x | (x,i) <- xs', p i x ]
              where xs' = zip xs [0..]

 

Thanks again.

I'm looking forward to the next lecture.

exoteric
exoteric
embarassingly sequential

I love IEnumerable<char> and recently used it for code-generation from XML to a simple Pascal-like language that generates the same XML - although I had some issue with it at first - as if Intellisense didn't recognize this fact that String : IEnumerable<char>.

 

I'm watching the ICFP videos every evening although quite a few of them are so deeply theoretical that you cannot understand them without quite some CS grounding. Nevertheless, there are also quite a few that are more accessible and show you where the future is, where the Haskell compiler development is, unusual uses of monads etc. So every interesting indeed.

 

Especially the oscar-winning mathematician from Industrial Light & Magic using monads for untangling knots, describing quantum computation, etc. - that was extremely interesting!

Here's the link for the ICFP 2009 videos.

 

Thanks for the great lecture series Erik. Converting the Haskell examples into their .NET equivalents really helps drive home some of the concepts for me. I also found your comparison between functional programming and the GOF design patterns quite enlightening.

 

I am waiting to see if Brian Beckman will "pop" in for the Monads lecture?

exoteric
exoteric
embarassingly sequential

Say monad thrice and the Beckman genie is bound by genie law to escape from his Klein bottle and do some monad tricks!

 

This is of course a lecture series but how wonderful it would be if there would be a few interruptions along the way but of course the Beckman genie is also busy

 

I loved his input on the IO E2E video where he showed in simplest terms what duality really means. The greatest minds find the simplest solutions. - And make them efficient.

 

Smiley

This is the first of the tutorials to really get into the "meat" of Haskell and I enjoyed it quite a bit (especially the fact that I was able to write the solutions with only a few minor missteps in syntax). I may become a Haskell convert: writing type signatures does make the code that follows much easier to construct.

Richard.Hein
Richard.Hein
... my guitar gently weeps ...

I take it you meant Enumerable.Range, not Observable.Range? 

exoteric
exoteric
embarassingly sequential

I bet that was on purpose, to show off the new Observable extensions to .Net. Even if that was probably not especially suitable in this case, there's no reason why it wouldn't apply equally well to an IO as an IE, or so it appears. - Speaking of observations! (Prettified observations)I am starting to see why looking at types is so satisfying! A pure structural/type-level examination of semantics is very fascinating. Looking at currying, lists and tuples in this light will be enlightening.

Head.In.TheBox
Head.In.TheBox
Head In The Box

From http://www.haskell.org/onlinereport/list.html

 

17.4  unfoldr

The unfoldr function is a "dual" to foldr: while foldr reduces a list to a summary value, unfoldr builds a list from a seed value. For example: 

  iterate f == unfoldr (\x -> Just (x, f x))

In some cases, unfoldr can undo a foldr operation: 

  unfoldr f' (foldr f z xs) == xs

if the following holds: 

  f' (f x y) = Just (x,y)
  f' z       = Nothing

Microsoft Communities