I'm writing a function that remove white space in a json string. I
need to know if the current char I'm processing is surrounded by "
, or
if it is after a escape char \
. So I need two more param for this
function.
Here is the current implementation. But I don't think it is lazy. How could I make it lazy with "filter" or "map" on the json string?
compressJson :: String -> String
compressJson json = compress json False False ""
-- compress params: json, inStr, aferEscape, acc
where compress [] _ _ acc = acc
compress ('\"' : xs) inStr False acc = compress xs (not inStr) False (acc ++ "\"")
compress ('\\' : xs) inStr False acc = compress xs inStr True acc
compress (x : xs) inStr True acc = compress xs inStr False (acc ++ ['\\', x])
compress (x : xs) True False acc = compress xs True False (acc ++ [x])
compress (x : xs) False _ acc = compress xs False False (acc ++ parse x)
parse c = if c `elem` " \t\n"
then []
else [c]