如何在 haskell 中使用列表理解编写函数?
how to write a function using list comprehension in haskell?
我已经使用 map
编写了此函数,但我需要使用列表理解来编写此函数:
alter = map (\x -> if x == 0 then 1 else 0)
它给出了例如
alter [1,1,0]
> [0,0,1]
你不能使用列表推导来写它:
alter xs = [if x == 0 then 1 else 0 | x <- xs]
我已经使用 map
编写了此函数,但我需要使用列表理解来编写此函数:
alter = map (\x -> if x == 0 then 1 else 0)
它给出了例如
alter [1,1,0]
> [0,0,1]
你不能使用列表推导来写它:
alter xs = [if x == 0 then 1 else 0 | x <- xs]