data Term = TNull -- null term
| TBool Bool -- boolean data
| TInt Int -- integer data
| TSymbol String -- symbolic data
| TPair Term Term -- pairs
| TVar LVar -- logic variable
deriving (Show, Eq, Ord)
walk :: Term -> Substitution -> Term
walk term substitution = undefined
type Substitution = Map.Map LVar Term
我要检查term
是否为逻辑变量。
如果它不是逻辑变量或不在Substitution映射中,则将其返回。否则,递归调用walk,以查看Substitution是否映射了另一个逻辑变量。我该如何实施?
正如AJFarmar所说,我尝试过对其进行模式匹配。
walk TVar substitution = if substitution Tvar
walk (substitution Tvar) substitution
walk _ substitution = TVar
但是似乎不起作用。
答案 0 :(得分:2)
使用模式匹配:
walk :: Term -> Substitution -> Term
walk term@(TVar lvar) sub = Map.findWithDefault term lvar sub
walk (TPair term1 term2) sub = TPair (walk term1 sub) (walk term2 sub)
walk term _ = term