zipWith Haskell

zipWith Haskell,haskell,Haskell,我已创建此数据类型和函数: type Bit = Int randomFloatList :: Int -> [Float] randomFloatList seed = randoms (mkStdGen seed) alter :: Bit -> Bit alter O = I alter I = O 我想创建一个使用zipWith的函数。该函数有一个种子作为参数,用于randomFloatList,如果随机元素介于0和noise之间,则该位将更改。我正试图这样做,但我在z

我已创建此数据类型和函数:

type Bit = Int

randomFloatList :: Int -> [Float]
randomFloatList seed = randoms (mkStdGen seed)
alter :: Bit -> Bit
alter O = I
alter I = O
我想创建一个使用zipWith的函数。该函数有一个种子作为参数,用于randomFloatList,如果随机元素介于0和noise之间,则该位将更改。我正试图这样做,但我在zipWith方面遇到了困难:

谢谢。

齐普威思的签名是a->b->c->[a]->[b]->[c]。这意味着它对于映射在两个列表上接受两个参数的函数非常有用

我认为,在您的例子中,您只需要在单个列表上映射函数,因为您的函数alterBit只接受一个参数,所以您必须使用map,而不是zipWith

另外请注意,除非您在模块中将种子和噪声定义为常量或显式地传递给alterBit,否则alterBit不会看到它们。

zipWith的签名是a->b->c->[a]->[b]->[c]。这意味着它对于映射在两个列表上接受两个参数的函数非常有用

我认为,在您的例子中,您只需要在单个列表上映射函数,因为您的函数alterBit只接受一个参数,所以您必须使用map,而不是zipWith


另外请注意,除非您在模块中将种子和噪声定义为常量,或将它们显式传递给alterBit,否则alterBit将看不到它们。

我相信您希望获取一个位列表,并使用一个随机列表来决定是否更改原始值。如果没有,请澄清

channel :: Int -> Float -> [Bit] -> [Bit]
channel seed noise xs = zipWith (alterBit noise) (randomFloatList seed) xs
请注意,您不需要一些括号-函数应用程序不需要括号,只需要分组

alterBit :: Float -> Float -> Bit -> Bit
alterBit noise random bit | random <= noise = alter bit
                          | otherwise = bit
只能想到一个alter函数:

type Bit = Int

randomFloatList :: Int -> [Float]
randomFloatList seed = randoms (mkStdGen seed)
alter :: Bit -> Bit
alter O = I
alter I = O
让我们测试一下:

> take 6 $ randomFloatList 3
[0.10321328,0.98988104,0.46191382,0.8553592,0.7980472,0.35561606]

> map (<= 0.5) $ take 6 $ randomFloatList 3
[True,False,True,False,False,True]

> channel 3 0.5 [O,O,O,O,O,O]
[I,O,I,O,O,I]

是的,这改变了它应该改变的部分。

我认为您需要一个位列表,并使用一个随机列表来决定是否更改原始位。如果没有,请澄清

channel :: Int -> Float -> [Bit] -> [Bit]
channel seed noise xs = zipWith (alterBit noise) (randomFloatList seed) xs
请注意,您不需要一些括号-函数应用程序不需要括号,只需要分组

alterBit :: Float -> Float -> Bit -> Bit
alterBit noise random bit | random <= noise = alter bit
                          | otherwise = bit
只能想到一个alter函数:

type Bit = Int

randomFloatList :: Int -> [Float]
randomFloatList seed = randoms (mkStdGen seed)
alter :: Bit -> Bit
alter O = I
alter I = O
让我们测试一下:

> take 6 $ randomFloatList 3
[0.10321328,0.98988104,0.46191382,0.8553592,0.7980472,0.35561606]

> map (<= 0.5) $ take 6 $ randomFloatList 3
[True,False,True,False,False,True]

> channel 3 0.5 [O,O,O,O,O,O]
[I,O,I,O,O,I]

是的,这改变了它应该改变的那些。

我们在谈论什么困难?提供更多的代码,让我们确定哪些失败了。我们讨论的困难是什么?提供更多的代码,让我们确定哪些失败。我找到了正确的解决方案。谢谢你的解释,这很有帮助。我找到了正确的解决方案。谢谢你的解释,这很有帮助。