20-CS-4003-001 Organization of Programming Languages Fall 2018
Functors

Lambda calculus, Type theory, Formal semantics, Program analysis

Prev     Next     All lectures           Code

Basics

 ```{- The a is the type parameter. Maybe is a type constructor. This type constructor can produce a type of Maybe_ Int, Maybe_ Car, Maybe_ String, etc. No value can have a type of just Maybe_ because Maybe_ is not a type. Maybe is a Functor - definition of a Functor is given on a later slide. -} data Maybe_ a = Nothing_ | Just_ a {- try :t on the following -} j1 = Just_ 32 j2 = Just_ "John" {- The type of Nothing is Maybe a. If some function requires a Maybe Int as a parameter, we can give it a Nothing, because a Nothing doesn't contain a value anyway and so it doesn't matter. -} {- 3 dimensional vector -} data Vector a = Vector a a a deriving (Show) {- can operate on many different type vectors -} vplus :: (Num t) => Vector t -> Vector t -> Vector t (Vector i j k) `vplus` (Vector l m n) = Vector (i+l) (j+m) (k+n) vp :: (Num a) => Vector a -> Vector a -> Vector a vp (Vector i j k) (Vector l m n) = Vector (i+l) (j+m) (k+n) vectMult :: (Num t) => Vector t -> t -> Vector t (Vector i j k) `vectMult` m = Vector (i*m) (j*m) (k*m) vm :: (Num a) => Vector a -> a -> Vector a vm (Vector i j k) m = Vector (i*m) (j*m) (k*m) scalarMult :: (Num t) => Vector t -> Vector t -> t (Vector i j k) `scalarMult` (Vector l m n) = i*l + j*m + k*n x1 = Vector 1 2 3 x2 = Vector 4 5 6 x3 = Vector 4.3 5.3 7.7 r1 = x1 `scalarMult` x2 r2 = x1 `scalarMult` x3 ```