我有以下情况:
let private runStatement (vars : Map<identifier, value>) stmt =
match stmt with
| Assignment (id, expr) -> runAssignment vars id expr
| Print exprs -> runPrint vars exprs
| Read id -> runRead vars id
| If (cond, stmts) -> runIf vars cond stmts
let rec private runStatements vars stmts =
match stmts with
| stmt::rest ->
let newVars = runStatement vars stmt
runStatements newVars rest
| [] -> vars
let private runIf vars conditionalValue statements =
match conditionalValue with
| Boolean v when v -> runStatements vars statements
| Boolean v -> vars
| _ -> failwith "Not a boolean expression in if statement"
如您所见,函数runStatement
调用runIf
和runIf
调用runStatement
,因为if语句是由一些通用语句和一般语句组成的可以是if语句。
我该如何解决这种情况?
PS:我和runWhile
,runIfElse
等其他函数有类似的情况。
答案 0 :(得分:8)
使用&#39;和&#39;关键字
let rec runx () =
printf "runx"
runy ()
and runy () =
printf "runy"
runx ()
runx () |> ignore
打印
runxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxruny