Haskell 2, Spring 20

Propose:  To learn the functional style of programming. 
Records, lists, user types, function types and local definitions will be discussed.

  1. Introductory examples: SimpleExamples.lhs
  2. Examples: Notes.lhs
  3. Haskell/Type basics 

Haskell  --  "=" and "::"

>   py :: Float
>   py = 3.14159
See Haskell Lab 1.


Type system

Types and values

Predefined types

Basic types

Structured types Lists and Tuples

Lists

Strings 

List operations

Tuples

User-defined types

Product types

> type Name = String
> type Age = Int
> data People = Person Name Age
> x = Person "John" 25
> showPerson :: People -> String 
> showPerson (Person n a) = n ++ " aged " ++ show a

Polymorphic data types

Point is a polymorphic data type: it works for any type 'a'
> data Point a = Pt a a
> firstCoord :: Point a -> a
> firstCoord (Pt m n) = m
Tree is a recursive polymorphic data type:

> data Tree a = Leaf a | Branch (Tree a) (Tree a)

> treeSize :: Tree a -> Integer
> treeSize (Leaf x) = 1
> treeSize (Branch t1 t2) = treeSize t1 + treeSize t2


Function types

Type inference  



Function composition


Currying

Currying a function

Function application in a curried language


Regular logs from general logs


Using where clause

Using let expressions

> twice x = x + x
> twice = (\x -> x + x)

twice ( 3 * 3)
~> let x = 3*3 in x + x
~> let x = 9 in x + x
~> 9 + 9
~> 18