​Parallel and Concurrent Haskell (3)

   

3.1 Algebraic Data Types: Product Type, Sum Type

Projection by Pattern Match: 

fst :: (a , b )  -> a
fst  (x, _) = x

snd (_,y) = y

:t  ( , ) data constructor 

( , ) :: a -> b -> (a,b) type constructor

Kind 
:k (,) type constructor 
(,) :: * -> * ->  *  

[* all haskell types]

Laziness

3.2 Sum Type

data Either a b = Left a | Right b
Left :: a -> Either a b
Right :: b -> Either a b

safeSqrt :: Either String Double -> Either String Double

safeSqrt (Left str) = Left str
safeSqrt (Right x) =
     if x <0 
         then Left “Error”
         else Right (sqrt x)

safeSqrt  sx = 
 case sx of
    Left str -> Left str
    Right str ->if x < 0
                       then Left “Error”
                       else Right (sqrt x)

data Bool = True | False
case x < 0 of
  True -> …
  False -> …

(<) :: Ord a => a -> a -> Bool

Note: Ord = Order

a + 0 = a

data X a = X a | Y Void

                       a + 0

a * 1 = a

type Y a = (a , () )

a * 0 = 0
type Z a = (a, Void) ~ Void

Note: (a, Void) is an impossible pair of 2 elements, because Void has no element, so (a, Void) is equivalent to Void.

2 = 1 + 1
2 = Bool
Bool = True | False

Parallel and Concurrent Haskell: 

Read Free Online Book:

http://chimera.labs.oreilly.com/books/1230000000929/index.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s