Haskell类似的类型复制粘贴消除以防万一

时间:2016-01-06 02:00:56

标签: haskell coding-style

case x of 
  Cond expr stmt -> do
        checkExprType expr Bool
        processStmt env stmt
        return env
  AnotherCond expr stmt -> do
        checkExprType expr Bool
        processStmt env stmt
        return env

如何消除此复制粘贴?

1 个答案:

答案 0 :(得分:4)

你可以制作一个本地定义,它是exprstmt的函数:

-- I assume this is all inside a 'do' block
let conditionalLike expr stmt = do
        checkExprType expr Bool
        processStmt env stmt
        return env
case x of
    Cond expr stmt -> conditionalLike expr stmt
    AnotherCond expr stmt -> conditionalLike expr stmt