小编Uns*_*bra的帖子

避免 Clojure 中的递归导致堆栈溢出

我是 Clojure 的新手,无法弄清楚如何在某些情况下避免堆栈溢出。在尝试使用我发现的名为kern 的解析器组合器库将解析项目移植到 Clojure 时,出现了一种这样的情况。

Kern 定义了“many-till”解析器的递归实现:source

这对于小输入来说效果很好:

(def input-text "The blue {cat} and the red {dog} became best friends with the white {wolf} END {not included}")

(def  between-brackets "parser that grabs all text between brackets"
  (between (sym* \{) (sym* \}) (<+> (many (none-of* "}")))))

(def parse-enclosed-words "parser that attempts to grab text between brackets, 
skips a character when it can't, and halts when it hits the string END"
  (many-till (<|> between-brackets (skip any-char)) (token* "END"))) …
Run Code Online (Sandbox Code Playgroud)

recursion parsing clojure

5
推荐指数
1
解决办法
198
查看次数

标签 统计

clojure ×1

parsing ×1

recursion ×1