java字符串除法函数,java – 函数式编程:如何处理函数式编程中的异常或它的等价物...
以下顯示了如何在Haskell中完成它.
基于類型siginure divide :: Int – > Int – >無論[Char] Int,您都可以看到函數(shù)除法將返回Left字符串或Right Int.
要么是代數(shù)數(shù)據(jù)結構,還有更多,你可以自己編寫.
divide :: Int -> Int -> Either [Char] Int
divide dividend divisor
| (divisor == 0) = Left "Sorry, 0 is not allowed :o"
| (dividend == (minBound :: Int)) && (divisor == -1) = Left "somethig went wrong"
| otherwise = Right (dividend `div` divisor)
main = do
print (divide 4 2) -- Right 2
print (divide 4 0) -- Left "Sorry, 0 is not allowed :o"
print (divide (minBound :: Int) (-1)) -- Left "somethig went wrong"
在Haskell中,您可以將錯誤拋出錯誤“和您的錯誤消息”,但這會讓您崩潰程序……這不是我們想要的.
總結
以上是生活随笔為你收集整理的java字符串除法函数,java – 函数式编程:如何处理函数式编程中的异常或它的等价物...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab slider不可移动,GU
- 下一篇: 彻底关闭Win10自动更新的代码