Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 从列表中创建列表,同时更改一个始终不同的值Haskell_List_Haskell_Int - Fatal编程技术网

List 从列表中创建列表,同时更改一个始终不同的值Haskell

List 从列表中创建列表,同时更改一个始终不同的值Haskell,list,haskell,int,List,Haskell,Int,我有一个值为1,-1,0的9个整数的列表,例如: [-1, 0, 0, 1, -1, -1, 1, 1, 0] 我想做的是从这个列表中创建一个列表,其中每个列表只包含一个更改,并且始终不同。对于每个-1,我想将其更改为0 例如: 从列表中: [-1,0,0,1,-1,-1,1,1,0], 我想得到结果: [ [ 0, 0, 0, 1, -1, -1, 1, 1, 0] , [-1, 0, 0, 1, 0, -1, 1, 1, 0] , [-1, 0, 0, 1, -1, 0, 1,

我有一个值为1,-1,0的9个整数的列表,例如:

[-1, 0, 0, 1, -1, -1, 1, 1, 0] 
我想做的是从这个列表中创建一个列表,其中每个列表只包含一个更改,并且始终不同。对于每个-1,我想将其更改为0

例如:

从列表中:

[-1,0,0,1,-1,-1,1,1,0], 
我想得到结果:

[ [ 0, 0, 0, 1, -1, -1, 1, 1, 0]
, [-1, 0, 0, 1,  0, -1, 1, 1, 0]
, [-1, 0, 0, 1, -1,  0, 1, 1, 0]
]

因此,每个列表只更改了一个值,并且每个列表都有一个不同的值。我甚至不知道如何开始

您始终需要做的第一件事是找出函数的类型签名。在你的情况下,你想要

lister :: [Int] -> [[Int]]
然后,当您想循环浏览列表,但要跟踪已更改的索引时,一种简单的方法是列出难以遵循的列表列表,只需查看代码,然后使用其索引将其压缩。然后为每个列表切换该位置的元素。这是你的密码

lister :: [Int] -> [[Int]]
lister ls = [switch i l | (i,l) <- zip [0..9] (repeat ls)]
另一种尝试:

zeros :: [Int] -> [Int] -> [[Int]]
zeros _ []     = []
zeros h (x:xs) = [h ++ newX:xs] ++ zeros nextH xs
    where newX = if x == (-1) then 0 else x
        nextH = h ++ [x]

switch xs = ((filter (/= xs)) . (zeros [])) xs
用法:

main = print $ switch [-1, 0, 0, 1, -1, -1, 1, 1, 0]

显然,这是一个非常具体的问题。从更大的角度来看是很有用的:这是一个什么更普遍的问题?显然,在这里,我们正在浏览一个列表,可能会看到我们希望以零或更多方式替换的元素。此外,我们希望看到有多少种方式可以进行数量有限的此类替换。因此,在考虑如何专门解决原始问题之前,让我们先实施一般情况:

import Control.Applicative (Alternative, empty, (<|>))

replaceNTimes :: Alternative f => (a -> f a) -> Int -> [a] -> f [a]
replaceNTimes _ 0 xs = pure xs
replaceNTimes _ _ [] = empty
replaceNTimes f n (x:xs) = replaceHere <|> keepLooking
  where replaceHere = (:) <$> f x <*> replaceNTimes f (n - 1) xs
        keepLooking = (x:) <$> replaceNTimes f n xs
并进行测试以确保获得预期输出:

*Main> replaceSingleNegativeOneWithZero [-1,0,0,1,-1,-1,1,1,0]
[ [0,0,0,1,-1,-1,1,1,0]
, [-1,0,0,1,0,-1,1,1,0]
, [-1,0,0,1,-1,0,1,1,0]]

像在Haskell中一样开始:使用类型签名。在那之后,试着用这个类型签名来编写最愚蠢的函数,在你不知道该怎么做的地方留下空白。我相信如果你已经检查过它们与原来的不同,你就不需要这个核心了one@Lorenzo. 非常感谢。你是对的!我会改变that@MichaelLitchard我认为这是一个糟糕的问题,但你不可能在一个问题后就猜出某人是否是吸血鬼。这就是为什么我选择用正确的方式来写我自己的答案,希望其他可能看到这个问题的观众会感兴趣,但作为一个复制/粘贴作业的解决方案显然是无用的。@amalloy我想我的评论写得不太好。我更关心的是,我指的是整个网站。想象一下,如果我们沉溺于每一个懒惰的问题,显然是在寻找其他人来做这项工作,我们会变成什么样?
import Control.Applicative (Alternative, empty, (<|>))

replaceNTimes :: Alternative f => (a -> f a) -> Int -> [a] -> f [a]
replaceNTimes _ 0 xs = pure xs
replaceNTimes _ _ [] = empty
replaceNTimes f n (x:xs) = replaceHere <|> keepLooking
  where replaceHere = (:) <$> f x <*> replaceNTimes f (n - 1) xs
        keepLooking = (x:) <$> replaceNTimes f n xs
replaceSingleNegativeOneWithZero :: [Int] -> [[Int]]
replaceSingleNegativeOneWithZero = replaceNTimes go 1
  where go (-1) = [0]
        go _ = []
*Main> replaceSingleNegativeOneWithZero [-1,0,0,1,-1,-1,1,1,0]
[ [0,0,0,1,-1,-1,1,1,0]
, [-1,0,0,1,0,-1,1,1,0]
, [-1,0,0,1,-1,0,1,1,0]]