I am trying to write a generalised condition evaluator, similar to what the Lisp/Scheme people call cond, using quotations because they are the easiest way to get call-by-name semantics. I'm having trouble pattern-matching against the list cons operation, and can't seem to find out exactly how to represent it. Here's what I have so far:
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
The problem is with the Cons identifier in the first branch of the pattern match--it doesn't exist, and I can't figure out how to represent the list :: data constructor.
What is the correct way to pattern match against the list cons data constructor?
if - then - elif - else? - Fyodor Soikin<@ ... @>) are faster to type than functions (fun () -> ...), and can be destructured and manipulated more easily. As to the overall justification, I'm trying to justify that we don't need new syntax for github.com/fsharp/fslang-suggestions/issues/519 - Yawar