CIS 194: Intro. to Haskell
SMHUG - July 10, 2018
Agenda
- Why do I like/dislike Haskell?
- What's CIS 194?
- What's the plan?
- Development env. setup
- Prakash intros. Week 1 Content
Why I Like Haskell
Type Inference
main = scotty 3000 $ do
get "/:word" $ do
word <- param "word"
html ("<h1>Hello, " ++ word ++ "</h1>")
Typed Side-Effects
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)
MVar, Green Threads
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)
Channels
main = do
messages <- newChan
writeChan messages "unbounded"
writeChan messages "channels"
-- Read a message from the channel,
-- then output it.
putStrLn =<< readChan messages
Laziness (Streams for Free)
fibonacci :: [Int]
fibonacci = 0 : (next 1 1)
where next n m = n : (next m (n + m))
Laziness (Streams for Free)
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]
Type Classes
-- define a type class
class ToJSON a where
toJSON :: a -> Value
Type Classes
-- define a type class
class ToJSON a where
toJSON :: a -> Value
-- define a type
data Person { firstName :: Text
, lastName :: Text }
Type Classes
-- 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 ]
Type Classes
-- 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
Lots of New Things to Learn
- Separating deterministic from nondeterministic code
- Interesting, new, mathy abstractions
- How do we write programs where state is mostly on the call stack?
The Latin of FP
- RxWhatever (Erik Meijer)
- Java Generics (Philip Wadler et al.)
- JavaScript (promises, Ramda)
- Scala (for-comprehension)
- LINQ (Erik Meijer)
- more
Why I Don't Like Haskell
- Lots of interesting new things to learn
- Learning curve is very steep
- Poor IDE support (relative to Java)
Laziness and IO
-- 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 Extensions
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
CIS 194
Why CIS 194?
- Coursework is challenging, illustrative of concepts
- Great homework assignments
- According to me and many on /r/haskell, it's the best option out there
Things We've Changed
- Added a chapter on Quickcheck
- Added a bunch of tests
- Replaced the original IO and Monoid chapters with something from Fall '14 version
What's the Plan?
- Get together every-other week
- Volunteers present concepts applicable to next week's assignment
- Afterwards, discuss homework solutions
- Eat pizza
What's Expected of You?
- Show up
- Do the homework
- Volunteer to talk about a relevant topic (functors, monoids, lazy evaluation, Quickcheck, etc.)
- Don't wreck the bathrooms
Tracking Progress

Let's Get Started
- https://github.com/laser/cis-194-summer-2018
- Write your GitHub username on the whiteboard and I'll add you as a collaborator
- Clone repo, create branch whose name is your username, push to origin
- Check out README.md for OSX setup steps
- Make PRs to master adding new tests and bugfixes
CIS 194: Intro. to Haskell (July 10, 2018)
By laser
CIS 194: Intro. to Haskell (July 10, 2018)
- 9