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

Haskell similar types copy-paste elimination in case

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

这个复制粘贴怎么去掉?

你可以做一个局部定义,它是 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