1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
--------------------------------------------------------------------------------
-- | This library provides a type and combinators for show functions using
--   functional lists.
--
-- Author:  Bjoern Peemoeller
-- Version: April 2016
--------------------------------------------------------------------------------
module Text.Show ( ShowS
                 , showString, showChar, showParen, shows
                 ) where

-- | The type synonym `ShowS` represents strings as difference lists.
--   Composing functions of this type allows concatenation of lists
--   in constant time.
type ShowS = String -> String

-- | Prepend a string
showString :: String -> ShowS
showString s = (s ++)

-- | Prepend a single character
showChar :: Char -> ShowS
showChar c = (c:)

-- | Surround the inner show function with parentheses if the first argument
--   evaluates to `True`.
showParen  :: Bool -> ShowS -> ShowS
showParen True  s = showChar '(' . s . showChar ')'
showParen False s = s

-- | Convert a value to `ShowS` using the standard show function.
shows :: Show a => a -> ShowS
shows = showString . show