@@ -0,0 +1,31 @@
1+module Main
2+
3+data Format = End | FInt Format | FStr Format | FChar Char Format
4+
5+fromList : List Char -> Format
6+fromList Nil = End
7+fromList ('%' :: 'd' :: cs) = FInt (fromList cs)
8+fromList ('%' :: 's' :: cs) = FStr (fromList cs)
9+fromList (c :: cs) = FChar c (fromList cs)
10+
11+PrintfType : Format -> Type
12+PrintfType End = String
13+PrintfType (FInt rest) = Int -> PrintfType rest
14+PrintfType (FStr rest) = String -> PrintfType rest
15+PrintfType (FChar c rest) = PrintfType rest
16+
17+printf : (fmt: String) -> PrintfType (fromList $ unpack fmt)
18+printf fmt = printFormat (fromList $ unpack fmt) where
19+ printFormat : (fmt: Format) -> PrintfType fmt
20+ printFormat fmt = rec fmt "" where
21+ rec : (f: Format) -> String -> PrintfType f
22+ rec End acc = acc
23+ rec (FInt rest) acc = \i: Int => rec rest (acc ++ (show i))
24+ rec (FStr rest) acc = \s: String => rec rest (acc ++ s)
25+ rec (FChar c rest) acc = rec rest (acc ++ (pack [c]))
26+
27+test : String
28+test = printf "The %s is %d" "answer" 42
29+
30+main : IO ()
31+main = print test