SFIX - May 5, 2022
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
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 messages
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
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
-- 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
-- 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
-- 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
* you don't really have to do this