SMHUG - July 10, 2018
main = scotty 3000 $ do
  get "/:word" $ do
    word <- param "word"
    html ("<h1>Hello, " ++ word ++ "</h1>")sum :: (Int, Int) -> Int
sum (x,y) = x + y
sum2 :: (Int, Int) -> IO Int
sum2 (x,y) = do
  contents <- readFile "/secret"
  putStr contents
  return (x + y)main = do
    result <- newEmptyMVar
    forkIO (do
        sleepMs 5
        putStrLn "Calculated result!"
        putMVar result 42)
    putStrLn "Waiting..."
    value <- takeMVar result
    putStrLn ("The answer is: " ++ show value)main = do
    messages <- newChan
    writeChan messages "unbounded"
    writeChan messages "channels"
    -- Read a message from the channel, 
    -- then output it.
    putStrLn =<< readChan messagesfibonacci :: [Int]
fibonacci = 0 : (next 1 1)
  where next n m = n : (next m (n + m))fibonacci :: [Int]
fibonacci = 0 : (next 1 1)
  where next n m = n : (next m (n + m))
*Main> take 10 $ fibonacci
[0,1,1,2,3,5,8,13,21,34]-- define a type class
class ToJSON a where
  toJSON :: a -> Value-- define a type class
class ToJSON a where
  toJSON :: a -> Value
-- define a type
data Person { firstName :: Text
            , lastName :: Text }-- define a type class
class ToJSON a where
  toJSON :: a -> Value
-- define a type
data Person { firstName :: Text
            , lastName :: Text }
-- the type instantiates the type class
instance ToJSON Person where
  toJSON (Person f l) = object [ "firstName" .= f
                               , "lastName" .= l ]-- define a type class
class ToJSON a where
  toJSON :: a -> Value
-- define a type
data Person { firstName :: Text
            , lastName :: Text }
-- the type instantiates the type class
instance ToJSON Person where
  toJSON (Person f l) = object [ "firstName" .= f
                               , "lastName" .= l ]
-- program against type classes
createJSONResponse :: ToJSON o => o -> WebServiceResponse ByteString-- hGetContents :: Handle -> IO String
-- withFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
-- closes file handle before fileData is forced
wrong = do
    fileData <- withFile "test.txt" ReadMode hGetContents
    putStr fileData
-- forces fileData before lambda exits
right = withFile "test.txt" ReadMode $ \handle -> do
    fileData <- hGetContents handle
    putStr fileData{-# LANGUAGE DeriveFunctor              #-}
{-# LANGUAGE FlexibleContexts           #-}
{-# LANGUAGE GADTs                      #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings          #-}
{-# LANGUAGE ScopedTypeVariables        #-}