私は休暇中にハスケルに少し時間を費やしていますが、今は問題にぶつかりました。ブール値で構成されるリストがあり、整数のリストを取得して、対応する位置でブール値を反転する関数を作成しようとしています。
次のコードをGHCiにロードし、flipBits testBoard [1,0,2]を実行しようとすると、結果は[True、True、False、False]になります。これをflipBitstestBoard[1,2,0]で実行すると、結果は[True、True、True、False]になります。
結果がflipBitsに渡されるリスト内の番号の順序に依存しないようにしたいと思います(リスト内の0は実行を停止するようです)。私は何が間違っているのですか?
flipBit board which (x:xs)
| which == 0 = (not (board !! which)):xs
| otherwise = x:(flipBit board (which-1) xs)
flipBits board [] = board
flipBits board (x:xs) = flipBits (flipBit board x board) xs
testBoard = take 4 $ repeat False