- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我最近完成了一个生成字符串列表的项目,我想知道执行此操作的最佳方法。
字符串生成是上下文敏感的,以确定它是否可以接受(这是游戏中的一系列游戏,所以你必须知道最后一次游戏是什么)
我这样做的方法是使用一个传递上下文参数和术语的函数,如果它是可接受的,它递归地继续,如果不是它终止(因为没有更多的字符串可以接受。)函数还收到一个“长度”参数以确保它最终终止
基本上这是生成语言接受的每个可能的字符串(一定长度)。
现在,我已经开始工作了,甚至相当干净利落,但我想知道是否有更好的方法来做到这一点。具体来说,“状态机”monad 能否很好地生成上下文相关语法?或至少类似的东西?这个问题似乎很简单,想要启动类似 parsec 的东西,是否有其他结构可以有效地操纵语言?
如有任何想法,我们将不胜感激。
最佳答案
我觉得这个问题看起来很有趣,所以我尝试了几种不同的实现方法。下面的代码是最有前途的方法。我认为它解决了所描述的问题,尽管我不确定一些细节。
基本上它允许一种形式的上下文相关语法,但只是一种非常简单的形式,其中每个产生式只能依赖于前一个符号。下面的代码构建了一些组合器,这些组合器允许将产生式非常直接地编码为“生成器”,并在幕后处理长度限制。
type sym = Xa | Xb | Xc // The terminal symbols
type word = sym list // A string of terminals
type gen = int -> sym list seq // Generate words up to the given length
/// Passes the previous symbol to a generator, after checking the length.
let (+>) : sym -> (sym -> gen) -> gen =
fun x g l -> if l<=0 then Seq.empty
else seq { for xs in g x (l-1) -> x :: xs }
let nil _ = seq { yield [] } // Generate just the empty word
let (|||) xs ys l = Seq.append (xs l) (ys l) // Union of two generators
let notAccepted _ = Seq.empty // Don't generate anything
let tryAll g = Xa +> g ||| Xb +> g ||| Xc +> g // Run g starting with any sym
// Generators for non-terminals. Each takes the previous symbol as an argument,
// and generates a (possibly empty) sequence of lists of symbols.
let rec gR = function Xa -> Xb +> gS ||| Xc +> gR ||| nil
| Xc -> Xb +> gS | _ -> notAccepted
and gS = function Xb -> Xa +> gR | _ -> notAccepted
let genTop = tryAll gR // The top level generator begins with gR with any sym
let words = genTop 4 // Generate words up to length 4
// val words : seq<sym list>
// = seq [[Xa; Xb; Xa]; [Xa; Xc; Xb; Xa]; [Xa]; [Xc; Xb; Xa]]
关于f# - F# 中的上下文敏感数据处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3720455/
我正在遍历自定义数据对象列表,并以可分页的格式将数据输出到屏幕。每个项目都需要提供编辑详细信息(重定向到另一个 View )和删除数据的能力。为此,我需要传递某种 id 来标识将要更新或删除的记录。我
我们正在使用 React-Native 构建移动应用程序,需要将一些敏感数据(例如客户端 key /密码)嵌入到移动应用程序本身中。 在安全性方面(最难进行逆向工程),标准做法是什么?数据应该在 na
我们已经知道使用 char[] 而不是 String 来处理敏感数据的建议/做法。有多种原因。一种是在不再需要敏感数据后立即清理它们: char[] passwd = passwordProvider
我有一些敏感数据想在使用后直接清除。目前,敏感数据的形式是 NSString。 NSString 在我的理解中是不可变的,这意味着我无法真正清除数据。不过,NSMutableString 似乎更合适,
我想知道如何推送到我的 Github 存储库(我需要公开该存储库,以便向雇主展示),而不显示敏感信息。我正在使用 Eclipse/Java。 我制作了一个在 Chrome 中使用 Selenium w
我是一名优秀的程序员,十分优秀!