【发布时间】:2011-08-13 16:12:43
【问题描述】:
我可以使用and 关键字来设置相互递归的函数定义。我也可以将and 用于相互递归的类型,但是如果类型和函数之间存在相互递归的关系怎么办?我唯一的选择是让函数成为该类型的成员还是我也可以在这里使用类似于and 的东西?
编辑:添加一个简化的伪示例,希望能说明我正在尝试做的事情
// A machine instruction type
type Instruction = Add | CallMethod int (* method ID *) | ...
// A class representing a method definition
type MethodDef (fileName : string) =
member x.Params with get () = ...
member x.Body with get() =
let insts = readInstructions fileName
Array.map toAbstractInst insts
// a more abstract view of the instructions
and AbstractInstruction = AbstAdd | AbstCallMethod MethodDef | ...
// a function that can transform an instruction into its abstract form
let toAbstractInst = function
| Add -> AbstAdd
| CallMethod methodId -> AbstCallMethod (somehowResolveId methodId)
| ...
所以你可以在这里看到递归关系的建立非常间接:MethodDef AbstractInst AND MethodDef -> toAbstractInst -> AbstractInstruction(其中 -> 表示“依赖于”)
【问题讨论】:
标签: f# mutual-recursion