-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPrintf.hs
More file actions
30 lines (23 loc) · 708 Bytes
/
Printf.hs
File metadata and controls
30 lines (23 loc) · 708 Bytes
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
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeSynonymInstances #-}
type Accum = String -> String
type Format = String
class PrintfType r where
format :: Accum -> Format -> r
instance PrintfType String where
format = ($)
instance (a ~ ()) => PrintfType (IO a) where
format acc fmt = putStrLn $ acc fmt
instance (Show a, PrintfType r) => PrintfType (a -> r) where
format acc fmt a =
let (txt, '%' : fmt') = span (/= '%') fmt
acc' = acc . (txt ++) . (show a ++)
in format acc' fmt'
printf :: (PrintfType r) => Format -> r
printf = format id
main :: IO ()
main = do
printf "hello"
putStrLn $ printf "hello %!" "world"