有时候我会写这样的代码
solveLogic :: Int -> Int -> Int
solveLogic a b =
let
x = 1
brainiac
| a >= x = 1
| a == b = 333
| otherwise = 5
in
brainiac
每次我都急于写这些东西而没有不需要的“brainiac”功能,就像这样:
solveLogic :: Int -> Int -> Int
solveLogic a b =
let
x = 1
in
| a >= x = 1
| a == b = 333
| otherwise = 5
哪个代码更像是“Haskellish”。有没有办法做到这一点?
答案 0 :(得分:44)
是的,使用where
子句:
solveLogic a b
| a >= x = 1
| a == b = 333
| otherwise = 5
where
x = 1
答案 1 :(得分:11)
当我想要守卫作为表达时,我会使用这种有点难看的黑客
case () of
_ | a >= x -> 1
| a == b -> 333
| otherwise -> 5