7

为了理解函数式编程的能力,我整理了一些基本函数,您可以将它们组合在一起以构建复杂的正则表达式。现在经过一些测试,我发现这是可行的,但是您可以用任何可行的语言编写一些可怕的代码。这是您会找到专业 F# 程序员编写的那种代码,还是我在滥用该功能?

注意: test就是我所指的具体内容。

type State = { input:string; index:int; succeeded:bool }
type Matcher = State -> State

let term (cs:char Set)  =
    fun s ->
        if s.succeeded && s.index < s.input.Length && cs.Contains s.input.[s.index] then  
            { input = s.input; index = s.index + 1; succeeded = true }
        else 
            { input = s.input; index = s.index; succeeded = false }

let quantify (term, min, max) =
    let rec inner (s:State, count) =
        if s.succeeded && s.index < s.input.Length && count <= max then
            inner (term { input = s.input; index = s.index + 1; succeeded = true }, count + 1) 
        elif count >= min && count <= max then
            { input = s.input; index = s.index - 1; succeeded = true }    
        else 
            s         
    fun s -> inner (s, 0) 

let disjunction leftTerm rightTerm =
    fun s ->
        let left = leftTerm s
        if not left.succeeded then
            let right = rightTerm s  
            if not right.succeeded then
                { input = s.input; index = s.index; succeeded = false }
            else
                right
        else
            left 

let matcher input terms =
    let r = terms  { input = input; index = 0; succeeded = true } 
    if r.succeeded then r.input.Substring (0, r.index) else null

let test = // (abc|xyz)a{2,3}bc
    disjunction // (abc|xyz)
        (term (set "a") >> term (set "b") >> term (set "c"))
        (term (set "x") >> term (set "y") >> term (set "z"))  
    >> quantify (term (set "a"), 2, 3) // (a{2,3})
    >> term (set "b") // b  
    >> term (set "c") // c

let main () : unit =
    printfn "%s" (matcher "xyzaabc" test)
    System.Console.ReadKey true |> ignore

main()
4

2 回答 2

8

代码对我来说看起来不错。

我不确定这是您的意图还是巧合,但是您正在实现与“解析器组合器”非常相似的东西,这是许多学术论文的主题:-)。我认为Monadic Parser Combinators非常易读(它在 Haskell 中有示例,但您应该能够将它们翻译成 F#)。

关于函数组合运算符。我一般不喜欢过多地使用运算符,因为它经常混淆代码。但是,在您的示例中,这很有意义,因为您可以很容易地想象这>>意味着“这个组应该被那个组跟随”,这很容易解释。

我要做的唯一小改动是为操作选择一些不错的自定义运算符disjunction并定义更多原始操作,以便您可以编写例如:

// Test against several terms in sequence
let sequence terms = (fun state -> terms |> Seq.fold (>>) state)
// Test for a substring
let substring s = sequence [ for c in s -> term (set [c]) ]

let test = // (abc|xyz)a{2,3}bc 
  ( substring "abc" <|> substring "xyz" )
  >> quantify 2 3 (term (set "a")) // (a{2,3}) 
  >> substring "bc" // bc

这是更高级别的描述,因此它删除了一些>>运算符,以支持更具描述性(和封装>>)的函数。我还更改quantify为采用多个参数而不是三倍(这是一个小改动)

如果您想进一步玩这个,那么您可以查看文章并尝试编写允许您使用parser { .. }语法的 F# 计算表达式构建器。

于 2010-09-01T03:02:07.223 回答
3

这通常是一种很好的风格,但是你缺少一些技巧并且仍然有相当多的冗余。也许更像这样:

let valid (s: State) = s.succeeded && s.index < s.input.Length
...
let disjunction leftTerm rightTerm s =
  let left = leftTerm s
  if left.succeeded then left else
    let right = rightTerm s  
    if right.succeeded then right else
      { s with succeeded = false }
...
let test =
  let f s = set s |> term
  let (++) s t = f s >> f t
  disjunction ("a" ++ "b" ++ "c") ("x" ++ "y" ++ "z")  
  >> quantify (f "a", 2, 3)
  >> "b" ++ "c"

您可能更喜欢累积一个表示计算的值而不是闭包,因为它使调试更容易。

于 2010-09-01T11:51:22.230 回答