eXi*_*nCe 3 logic haskell if-statement conditional-statements
我在以下练习中遇到问题:
编写一个接收三个Ints的函数,如果它们都是正数则求和,否则返回0(零).
我所做的是以下内容:
sum' :: int -> int -> int -> int
sum' x y z = if x >= 0, y >= 0, z >= 0 then x+y+z else 0
Run Code Online (Sandbox Code Playgroud)
我不知道如何使一个多个条件,如果,不知道这是否与逻辑"连接器"(如完成||或&&在Java中),或者如果在以类似的方式,我写的代码来完成.
它可以通过多种方式完成.
例如,使用&&:
sum' :: Int -> Int -> Int -> Int
sum' x y z = if x >= 0 && y >= 0 && z >= 0 then x+y+z else 0
Run Code Online (Sandbox Code Playgroud)
或使用all和列表:
sum' :: Int -> Int -> Int -> Int
sum' x y z = if all (>= 0) xs then sum xs else 0
where xs = [x, y, z]
Run Code Online (Sandbox Code Playgroud)