Skip to content

Instantly share code, notes, and snippets.

@chrisdone
Last active March 29, 2019 09:49
Show Gist options
  • Save chrisdone/57bd9456e43d3dab84e45e504af57f08 to your computer and use it in GitHub Desktop.
Save chrisdone/57bd9456e43d3dab84e45e504af57f08 to your computer and use it in GitHub Desktop.
Real Haskell Program Samples
#!/usr/bin/env stack
-- stack --resolver lts-12.12 script
import Options.Applicative.Simple
import Data.Semigroup ((<>))
data Sample =
Sample
{ sampleEnable :: Bool
, sampleUrl :: String
} deriving (Show)
main = do
(opts, ()) <-
simpleOptions
"1.0"
"Demo opts program"
"This program demonstrates commandline options."
(Sample
<$> flag False True (long "enable-the-thing" <> short 'e' <> help "Enable it!")
<*> strArgument (metavar "URLHERE" <> help "The URL"))
empty
print opts
#!/usr/bin/env stack
-- stack --resolver lts-12.12 script
{-# LANGUAGE OverloadedStrings #-}
import Data.Aeson
main = do
print (decode "{\"age\":50,\"name\":\"Sofia\"}" :: Maybe Value)
print (encode out)
out :: [(Int, String)]
out = [(1,"Hello"),(2,"World!")]
#!/usr/bin/env stack
-- stack --resolver lts-12.12 script
{-# LANGUAGE OverloadedStrings #-}
import Network.HTTP.Simple
import qualified Data.ByteString.Char8 as B8
main :: IO ()
main = do
response <- httpBS "http://example.com"
B8.putStrLn (getResponseBody response)
#!/usr/bin/env stack
-- stack --resolver lts-12.12 script
{-# LANGUAGE OverloadedStrings #-}
import Network.Wai
import Network.HTTP.Types
import Network.Wai.Handler.Warp (run)
app :: Application
app _ respond = do
putStrLn "I've done some IO here"
respond
(responseLBS
status200
[("Content-Type", "text/plain")]
"Hello, Web!")
main :: IO ()
main = do
putStrLn "http://localhost:8080/"
run 8080 app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment