Skip to content

Instantly share code, notes, and snippets.

@mtolly
Created April 12, 2019 00:44
Show Gist options
  • Save mtolly/a077a6a0bb91f4f044ca0222fa8e0ce9 to your computer and use it in GitHub Desktop.
Save mtolly/a077a6a0bb91f4f044ca0222fa8e0ce9 to your computer and use it in GitHub Desktop.
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}
module Main where
import Control.Monad.Trans.Writer
import Data.String (IsString (..))
newtype ListBuilder e a = ListBuilder { runListBuilder :: Writer [e] a }
deriving (Functor, Applicative, Monad)
instance (IsString e, a ~ ()) => IsString (ListBuilder e a) where
fromString s = ListBuilder $ tell [fromString s]
el :: e -> ListBuilder e ()
el x = ListBuilder $ tell [x]
buildInfo :: Config -> String
buildInfo config = unlines $ execWriter $ runListBuilder $ do
"************** INFORMATION **************"
"Welcome to the Hydraulic Press Channel."
"Here is your configuration:"
""
el $ " depth: " ++ show (depth config)
el $ " width: " ++ show (width config)
""
"*****************************************"
data Config = Config { depth :: Int, width :: Int }
main :: IO ()
main = putStrLn $ buildInfo defaultConfig
defaultConfig :: Config
defaultConfig = Config { depth = 5, width = 10 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment