F# 模式匹配列表

F# 模式匹配列表,f#,F#,我试图编写一个通用的条件求值器,类似于Lisp/Scheme人们所说的,使用引号,因为它们是获取按名称调用语义的最简单方法。我在与list cons操作进行模式匹配时遇到问题,似乎无法准确地找到如何表示它。以下是我目前掌握的情况: open FSharp.Quotations.Evaluator open Microsoft.FSharp.Quotations open Microsoft.FSharp.Quotations.Patterns let rec cond = function

我试图编写一个通用的条件求值器,类似于Lisp/Scheme人们所说的,使用引号,因为它们是获取按名称调用语义的最简单方法。我在与list cons操作进行模式匹配时遇到问题,似乎无法准确地找到如何表示它。以下是我目前掌握的情况:

open FSharp.Quotations.Evaluator
open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Quotations.Patterns

let rec cond = function
  | NewUnionCase (Cons, [NewTuple [condition; value]; tail]) ->
    if QuotationEvaluator.Evaluate <| Expr.Cast(condition)
      then QuotationEvaluator.Evaluate <| Expr.Cast(value)
      else cond tail

  | _ -> raise <| MatchFailureException ("cond", 0, 0
打开FSharp.quotes.Evaluator
打开Microsoft.FSharp.quotes
打开Microsoft.FSharp.quotes.Patterns
设rec cond=函数
|NewUnionCase(Cons,[NewTuple[条件;值];tail])->

if QuotationEvaluator.Evaluate我不认为有任何简单的方法可以直接在模式中写入
Cons
,但是您可以使用
when
子句来检查联合案例是否是
列表中名为
“Cons”
的案例类型:

let rec cond = function
  | NewUnionCase (c, [NewTuple [condition; value]; tail]) 
      when c.Name = "Cons" && c.DeclaringType.IsGenericType &&
        c.DeclaringType.GetGenericTypeDefinition() = typedefof<_ list> -> 
      Some(condition, value, tail)
  | _ -> 
      None
let rec cond=函数
|NewUnionCase(c,[NewTuple[条件;值];尾部])
当c.Name=“Cons”&&c.DeclaringType.IsGenericType&&
c、 DeclaringType.GetGenericTypeDefinition()=typedefof->
一些(条件、价值、尾部)
| _ -> 
没有一个

为什么要引用?为什么不使用函数呢?想想看,为什么要实现这样一个东西呢?
if-then-elif-else
有什么问题?@FyodorSoikin,引号(
)比函数(
fun()->…
)输入速度更快,并且可以更容易地进行分解和操作。至于整体的理由,我试图证明我们不需要新的语法