Posted By: Charles | Dec 7th, 2009 @ 4:38 PM | 42,062 Views | 8 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 10, Declaring Types and Classes, Dr. Meijer teaches us about type declarations, data declarations, arithmetic expressions, etc.  In Haskell, a new name for an existing type can be defined using a type declaration:

 type String = [Char]

 String is a synonym for the type [Char].

Like function definitions, type declarations can also have parameters. Type declarations can be nested, but not recursive.

Nested:
type Pos   = (Int,Int)

type Trans = Pos -> Pos


Illegal recursion:
type Tree = (Int,[Tree])

A completely new type can be defined by specifying its values using a data declaration:

 
data Bool = False | True

Bool is a new type, with two new values False and True.


Get the presentation slides here

Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Rating:
10
0

I'm so loving these lectures!

Thank you very much Charles and Erik for your work...you're doing a wonderful job!

I'm really looking forward to see the last three (or maybe four Smiley) lectures

Excellent job Erik and C !! We would love very much for you guys to cover some advanced topics in Haskell and Fuctional Programming (as i mentioned in chapter 9 thread ) Thanks and continue with the excellent work.

exoteric
exoteric
embarassingly sequential

Still watching this episode, I just had to say I love how you tied "Theorems for free!" together with dependent typing as well as ADTs!

In progress...

These lectures have been the highlight of my Thursdays. Smiley .  Thanks for taking the time to produce them. I’m looking forward to see this lecture tonight.

chudq
chudq
Life is beautiful, and sharing is wonderful!

Today there is big snow in Calgary. I am enjoying the lecture on my Mac. Here is my type: snow a = Nothing | Big snow. Thank you Eric!

Erik: You are talking about "the expression problem" as still being a research subject. However, multimethods (as defined in the language Clojure) elegantly solves the problem?

 

Eric, a very late question

 

In object oriented languages, we have the flexibility of defining sub-classes late after super class has been defined. The two definitions need not be done simultaneously. Moreover, there is no limit on the number of sub-classes baring the case of sealed types (which could still be circumvented using containment and defining casts).

In relation to the analogy of algebraic types and class hierarchies, what is the way in Haskell to extend the types as we can do in object oriented languages.

For example:

data Answer = No | Yes

data AnswerEx = FromAnswer Answer | Unknown

 

fromAnswerEx :: AnswerEx -> Answer

fromAnswerEx FromAnswer a = a

fromAnswerEx Unknown = No -- in Haskell we have to generate a value of Answer type here

 

We could surely define functions for explicit conversion e.g. fromAnswerEx but the compiles can not use them automatically. In code given below we have to use the explicit conversions.

 

answers = [Yes, No, fromAnswerEx Unknown, fromAnswerEx FromAnswer Yes]

 

The following will not work.

 

answers = [Yes, No, Unknown, FromAnswer Yes]

 

It is possible there in object oriented languages.

 

Is there any pattern/idiom in Haskell to achieve same?

 

Microsoft Communities