使用带有列表的守卫 haskell
using a guard with a list haskell
所以我有一个函数需要
otherfunction :: Int -> Bool
otherfunction = True
otherfunction
的实施无关紧要
function :: [Int] -> world -> world
function listOfInts World | [otherfunction x <- listOfInts] = world {alive = False}
| otherwise world
所以如果 listOfInts
returns 和 False
中的任何 otherfunction(x)
我想让它做那个世界 {alive is False} ,我该如何实现这个?我也许可以做 False
elem[otherfunction x <- listOfInts]
,在 Haskell.
中有更好的方法吗
感谢您的帮助,我是函数式编程的新手。
您可以将 world
设置为 &&
和此列表的 all otherfunction
,例如:
function :: [Int] -> World -> World
function listOfInts world = world {alive = alive world && allOther }
where allOther = <b>all otherfunction listOfInts</b>
所以从 listOfInts
中的一个元素 i
开始,其中 otherfunction i
returns False
, all otherfunction listOfInts
将 return False
,从而改变世界的alive
。
我们还可以使用条件表达式:
function :: [Int] -> World -> World
function listOfInts world | alive world && not allOther = world {alive = False }
| otherwise = world
where allOther = all otherfunction listOfInts
我们也可以省略 alive world
检查。但这意味着如果世界已经死了,我们可能会在列表中做很多进动,那是没有必要的。
您似乎在寻找 all
:
function :: [int] -> world -> world
function listOfInts world
| all otherfunction listOfInts = world
| otherwise = world {alive = false}
所以我有一个函数需要
otherfunction :: Int -> Bool
otherfunction = True
otherfunction
的实施无关紧要
function :: [Int] -> world -> world
function listOfInts World | [otherfunction x <- listOfInts] = world {alive = False}
| otherwise world
所以如果 listOfInts
returns 和 False
中的任何 otherfunction(x)
我想让它做那个世界 {alive is False} ,我该如何实现这个?我也许可以做 False
elem[otherfunction x <- listOfInts]
,在 Haskell.
感谢您的帮助,我是函数式编程的新手。
您可以将 world
设置为 &&
和此列表的 all otherfunction
,例如:
function :: [Int] -> World -> World
function listOfInts world = world {alive = alive world && allOther }
where allOther = <b>all otherfunction listOfInts</b>
所以从 listOfInts
中的一个元素 i
开始,其中 otherfunction i
returns False
, all otherfunction listOfInts
将 return False
,从而改变世界的alive
。
我们还可以使用条件表达式:
function :: [Int] -> World -> World
function listOfInts world | alive world && not allOther = world {alive = False }
| otherwise = world
where allOther = all otherfunction listOfInts
我们也可以省略 alive world
检查。但这意味着如果世界已经死了,我们可能会在列表中做很多进动,那是没有必要的。
您似乎在寻找 all
:
function :: [int] -> world -> world
function listOfInts world
| all otherfunction listOfInts = world
| otherwise = world {alive = false}