ERROR - C stack overflow in Haskell using Hugs

I am working on parsing a CSV file into a CSV type, which is a Record list, which is a list of fields that are just strings. After inserting a new line and then trying to access csv, I get c error. I read this error, perhaps from too much "thunk", using tail recursion, but I don’t think that what I am doing is wrong?

type CSV = [Record] type Record = [Field] type Field = String run :: IO() run = do inFile <- readFile "myFile.csv" let csv = parse inFile let csv = (insertRow "abc,def,ghi" csv) putStr (show csv) insertRow :: String -> CSV -> CSV insertRow newRow csv = csv ++ [toRecord newRow] parse :: String -> CSV parse file = map toRecord (parseLines file "" []) toRecord :: String -> Record toRecord line = parseWords line "" [] -- parseLine input partialCSV records parseLines :: String -> String -> [String] -> [String] parseLines [] partial records = records ++ [partial] parseLines ('\r':xs) partial records = parseLines xs [] (records ++ [partial]) parseLines (x:xs) partial records = parseLines xs (partial ++ [x]) records -- parseWords input partialRecord fields parseWords :: String -> String -> [String] -> [String] parseWords [] partial fields = fields ++ [partial] parseWords ('"':xs) partial fields = parseQuotes xs partial fields parseWords (',':xs) partial fields = parseWords xs [] (fields ++ [partial]) parseWords (x:xs) partial fields = parseWords xs (partial ++ [x]) fields parseQuotes :: String -> String -> [String] -> [String] parseQuotes ('"':xs) partial fields = parseWords xs [] (fields ++ [partial]) parseQuotes (x:xs) partial fields = parseQuotes xs (partial ++ [x]) fields 
+6
source share
2 answers

let the bindings be recursive so this line

 let csv = (insertRow "abc,def,ghi" csv) 

creates an infinite loop, you define csv in terms of yourself in a way that doesn't end there. Change it to

 let csv' = ... 

and type csv' in the next line.

+5
source

Double let csv = ... looks suspicious. Could you try to unravel the two variables? It probably doesn't do what you want (in Haskell let is recursive).

+2
source

Source: https://habr.com/ru/post/905107/


All Articles