Posted By: Charles | Oct 8th @ 8:33 AM | 54,540 Views | 59 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 2, Dr. Meijer introduces Haskell syntax and notation (via a Haskell implementation called Hugs, to be precise, which is based on Haskell 98) and we learn about the Haskell syntax that represents the fundamental construct of functional programming: functions. It's not like you're used to in mathematics like f(x). Instead, in Haskell, a function is denoted without parentheses: f x. So, given the almost OCD requirement by Haskell language designers to eliminate any unnecessary clutter in the language, parentheses are replaced by space. Also, in mathematics, you're accustomed to multiplication expressed either as xy or x y. In Haskell, since space denotes a function, multiplication is denoted with a *, like x*y...

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

Chapter 1

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

One amazing add-on for GHC is a stand-in frontend sort of a replacement for GHCi (using GHC officially) is a project called WinGHCi found http://code.google.com/p/winghci/  I fully recommend its usage since I haven't been disappointed by it yet (and it prints to screen ~20x faster than the original ghci output) Tongue Out

 

Really looking forward to studying my way through this! Big thanks to Erik and the C9 team again.

Very very interesting and a great lecture. 

 

Where can I find the slides? I want to do homework. please help:)

wil2300
wil2300
Super #

Sweet! Been looking forward to this all week. Still gotta get the book - I have an older version.

exoteric
exoteric
I : Next<I>

Hungarian notation: An artwork from hell. On the other hand, you are right, using this naming pattern, is kind of in the spirit of Hungarian notation, so I guess the hate-level has to be toned down a little, because it really is quite pretty. I don't quite like xs as much as s anymore because if you just say xs and you have to decompose it into locals in C#, then you need to save x and what's the rest of xs? xss? So in C# at least, without pattern matching it appears to be a little better with s decomposes to x and xs as in first of s and rest of s. I forget but seem to remember that you can pattern match a list in some languages using x:xs so here the parts are already decomposed into a pattern and you don't need the s to denote the whole, as the relevant parts have been named in the pattern. A reason for xs as name is also that it means you align well with the empty list case [].

 

The infix notation is not quite as nice as the prefix notation due to the quotes but in a proper editor, these could be removed from view and the infix operators could be specially formatted to reduce noise.

 

Object-oriented vs Function-oriented (aka Functional): Intellisense is nice but how about looking up locals based on what fits in the slots - if you pick product, then any local that fits into this will be shown. Well...

 

I love the short-code love and am reminded by sentence by Brian Beckman in a previous interview: "Shorter is almost always better." There Brian was talking about short as a more direct way to express simplicity; because shorter may be seen by some as more complex - the same is equally true for longer: imagine a short Haskell program compiled to X86 machine code, it too will be complex for many people, but as Brian says, everyone agrees on what short is.

 

I wasn't aware of the pervasiveness of transparently switching to "bignum" when necessary in these languages. That is quite beautiful. It adds another layer of transparency. You don't need to concern yourself with hardware limits of machine types.

 

One note on the presentation style: Erik you appear a little short of air at times, maybe thinking faster than you have air ready. (Well, and you kind of pronounce bloat as blood ;o) )

 

I wonder if ≤ and ≠ are legal function names in Haskell and defined in the Prelude. It would be nice - also, in this style discussion - because visually it makes the code more homogenous and aligned (≤ and > will take up the same space). And of course in C# we have to use if (..) else if (...) which completely messes up this homogeneity.

 

Excellent again! Smiley

exoteric
exoteric
I : Next<I>

OK, exercises. Defined it in C# after the first exercise but it's non-sensical for infinite lists - this is where non-termination (⊥) comes in handy

 

lastx xs = head (reverse xs)

 

... The |> looks like the solution to the problem of intellisense; when you write: [1,2,3] |> you are in a way making [1,2,3] the most important thing and |> becomes like the dot which fires off intellisense. And then ofcourse if you can use infix notation, Intellisense could help suggest functions as infixes given the first value (on the left) while the remaining values are unspecified.

Regarding the $-operator:

 

I've never heard any Haskell programmer say that the $-operator is used to save letters, because as Erik says, you only save at most one. The purpose is to avoid having expressions that end with a huge mountain of parenthesises. Compare the two following, slightly nonsensical, snippets.

 

 


print (take 10 (sort (map (\x -> x + 17) (filter isPrime xs)))))

 

print $ take 10 $ sort $ map (\x -> x + 17) $ filter isPrime xs

 

 

Notice how the second version reads alot more similar to when you write pipes in Unix or when you chain together method calls in an OO language.

 

Did you even notice that there was one end parenthesis to much in the first version?

 

 So the $ is not about saving characters, it's about clarity. But I agree that there is a general tendency among Haskellers to be overly clever (read: obscure) to write shorter code.

 

Microsoft Communities