Haskell:忽略一个案例或使用when

MCH*_*MCH 0 haskell if-statement yesod

我还有很多东西要学习Haskell,我在使用下面的代码时遇到了一些麻烦.我可以执行以下操作,但当然它会创建编译器警告,因为并非所有情况都显示在内case address s of,我想确保那些警告不存在:

postAddTenantR :: Handler Value
postAddTenantR = do
    newTenantData <- parseJsonBody :: Handler (Result NewTenant)
    case newTenantData of
      Success s -> runDB $ do
        newTenantGroup <- insert $ TenantGroup
        _ <- insert $ Tenant (firstName s) (lastName s) (Just newTenantGroup)

        case address s of
          Just newAddress -> do
            newProperty <- insert $ Property newAddress
            insert $ Lease Nothing Nothing Nothing newProperty newTenantGroup

        returnJson $ ProcessingResponse True Nothing
      Error e -> returnJson $ ProcessingResponse False (Just $ pack e)
Run Code Online (Sandbox Code Playgroud)

我不确定如何做类似Nothing -> do {}Nothing -> return ()匹配类型的事情.如果它没什么,我不想做任何事情.我也想过使用when但是再次编译器不喜欢返回类型:

when (isJust $ address s) $ do
  newAddress <- address s
  newProperty <- insert $ Property newAddress
  insert $ Lease Nothing Nothing Nothing newProperty newTenantGroup
Run Code Online (Sandbox Code Playgroud)

ham*_*mar 5

问题是case表达式的两个替代方案都需要具有相同的类型.由于您忽略了返回值,因此可以return ()Just案例中添加一个.那么您也应该能够return ()Nothing案例中使用:

case address s of
  Just newAddress -> do
    ...
    return ()
  Nothing -> return ()
Run Code Online (Sandbox Code Playgroud)