CIS 194: Intro. to Haskell
SFIX - May 5, 2022
Agenda
- I'm gonna talk about Haskell for a bit
- What's CIS 194?
- What's the plan?
- Development env. setup
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)
Cool Concurrency Primitives
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)
Cool Concurrency Primitives
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)
More Concurrency Stuff
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))
*Main> take 10 $ fibonacci
[0,1,1,2,3,5,8,13,21,34]
Open (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
Open (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
Open (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
Open (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 using monads
- Interesting, new, mathy abstractions
- Recursion schemes
- How do we write programs where state is mostly on the call stack?
Influenced Lots of Stuff
- RxWhatever (Erik Meijer)
- Java Generics (Philip Wadler et al.)
- JavaScript (promises, Ramda)
- Scala (for-comprehension)
- LINQ (Erik Meijer)
- more
Annoying Things
- Lots of interesting new things to learn
- Learning curve is very steep
- Poor IDE support (relative to e.g. Java)
- Lazy IO is a PITA
- Language extensions make every project different (like JavaScript?)
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 bunch of tests
- Marked a couple homework problems as optional
What's the Plan?
- Get together every week
- Compare homework solutions
- Ideally, somebody would volunteer to talk about the material relevant to the forthcoming assignment
What's Expected of You?
- Show up
- Do the homework
- Volunteer* to talk about a relevant topic (functors, monoids, lazy evaluation, Quickcheck, etc.)
* you don't really have to do this
Getting Started
- clone https://github.com/laser/cis-194-summer-2022
- follow setup steps in README.md to get your dev environment up and running
- share what you learn in the #programming-for-funzies chat
- come next week with the first homework complete
CIS 194: Intro. to Haskell (May 5, 2022)
By laser
CIS 194: Intro. to Haskell (May 5, 2022)
- 12