“错误 - C 堆栈溢出”在 Haskell 中使用 Hugs

发布于 2024-12-25 08:53:46 字数 1499 浏览 2 评论 0原文

我正在努力将 CSV 文件解析为 CSV 类型,它是一个记录列表,它是一个字段列表,只是字符串。插入新行然后尝试访问 csv 后,出现 c 堆栈溢出错误。我读过这个错误可能来自使用尾递归太大的“thunk”,但我不认为这就是我做错的地方?

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

I'm working on parsing a CSV file into a CSV type which is a list of Record which is a list of Field, which are just Strings. After inserting a new row and then trying to access the csv I get the c stack overflow error. I'v read this error may come from too large a "thunk" by using tail recursion but I don't think thats what I'm doing 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不即不离 2025-01-01 08:53:46

let 绑定是递归的,因此这一行

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

创建了一个无限循环,您以一种不会终止的方式自行定义 csv 。将其更改为

let csv' = ...

并在下一行中打印 csv'

let bindings are recursive, so this line

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

creates an infinite loop, you're defining csv in terms of itself in a way that doesn't terminate. Change it to

let csv' = ...

and print csv' in the next line.

一个人的旅程 2025-01-01 08:53:46

let csv = ... 看起来很可疑。你能尝试解开这两个变量吗?它可能不会做你想要的(在 Haskell 中 let 是递归的)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文