Haskell: 从元组中删除一个成员
Haskell: Deleting a member from a tuple
我正在尝试在 Haskell 中制作数独求解器,但在制作 deleteV 函数时遇到问题,如果相邻单元格具有相同的值,该函数将从候选列表中删除一个值。电路板和电池都是我定义的,下面是我目前的情况:
type Cell = (Int, [Int])
type Board = [Cell]
rowNo sq = sq `div` 9
colNo sq = sq `mod` 9
boxNo sq = (sq `div` 9 `div` 3 ) * 3
+ (sq `div` 3) `mod` 3
-- Two squares sq1 and sq2 are in the same neighborhood if their row,
-- column, or box numbers are the same.
isNeighbor sq1 sq2 =
((rowNo sq1) == (rowNo sq2)) ||
((colNo sq1) == (colNo sq2)) ||
(((boxNo sq1) == (boxNo sq2)))
delV val sq board =
if board == [] then []
else if isNeighbor (fst(head board)) sq -- then delV val sq (tail board)
then (val `delete` snd(head board)):(delV val sq (tail board))\
我可以让 delV 做它应该做的一部分,但不是全部。
当我用“delV 5 2 [(2, [1,5,7]), (26, [1,5,8])]
”调用 delV 时,我得到
“[[1,7],[1,5,8]]
”,但所需的输出是“[(2, [1,7]), (26, [1,5,8])]
”。虽然唯一的区别是在输出中包含索引,但我需要这种格式的索引。 P.S。当前版本的 delV 无法编译,在我尝试修复它的过程中,我又破坏了它。
已编辑:这是 delV 的一个版本,returns“[[1,7],[1,5,8]]
”是正确的,但不包括索引:
delV val sq board =
if board == [] then []
else if isNeighbor (fst(head board)) sq then val `delete` snd(head board):delV val sq (tail board)
else (snd(head board)):delV val sq (tail board)
您有一些格式错误的代码,您想要的输出有一个 7
,看起来您想要一个 8
。我假设这就是你的意思:
import Data.List
delV val sq board =
if board == [] then []
else if isNeighbor (fst(head board)) sq
then val `delete` snd(head board):delV val sq (tail board)
else (snd(head board)):delV val sq (tail board)
哪里
delV :: Int -> Int -> Board -> [[Int]]
delV 5 2 [(2, [1,5,7]), (26, [1,5,8])] == [[1,7],[1,5,8]]
如你所愿
delV :: Int -> Int -> Board -> Board
delV 5 2 [(2, [1,5,7]), (26, [1,5,8])] == [(2, [1,7]), (26, [1,5,8])]
您只需将索引加回去即可。就是这样,继续你的风格:
delV :: Int -> Int -> Board -> Board
delV val sq board =
if board == [] then []
else if isNeighbor (fst(head board)) sq
then (fst(head board), val `delete` snd(head board)):
delV val sq (tail board)
else head board:delV val sq (tail board)
这里是地图方面的:
delV2 :: Int -> Int -> Board -> Board
delV2 value square = map f
where
f (index, candidates) | isNeighbor index square =
(index, delete value candidates)
f t = t
我正在尝试在 Haskell 中制作数独求解器,但在制作 deleteV 函数时遇到问题,如果相邻单元格具有相同的值,该函数将从候选列表中删除一个值。电路板和电池都是我定义的,下面是我目前的情况:
type Cell = (Int, [Int])
type Board = [Cell]
rowNo sq = sq `div` 9
colNo sq = sq `mod` 9
boxNo sq = (sq `div` 9 `div` 3 ) * 3
+ (sq `div` 3) `mod` 3
-- Two squares sq1 and sq2 are in the same neighborhood if their row,
-- column, or box numbers are the same.
isNeighbor sq1 sq2 =
((rowNo sq1) == (rowNo sq2)) ||
((colNo sq1) == (colNo sq2)) ||
(((boxNo sq1) == (boxNo sq2)))
delV val sq board =
if board == [] then []
else if isNeighbor (fst(head board)) sq -- then delV val sq (tail board)
then (val `delete` snd(head board)):(delV val sq (tail board))\
我可以让 delV 做它应该做的一部分,但不是全部。
当我用“delV 5 2 [(2, [1,5,7]), (26, [1,5,8])]
”调用 delV 时,我得到
“[[1,7],[1,5,8]]
”,但所需的输出是“[(2, [1,7]), (26, [1,5,8])]
”。虽然唯一的区别是在输出中包含索引,但我需要这种格式的索引。 P.S。当前版本的 delV 无法编译,在我尝试修复它的过程中,我又破坏了它。
已编辑:这是 delV 的一个版本,returns“[[1,7],[1,5,8]]
”是正确的,但不包括索引:
delV val sq board =
if board == [] then []
else if isNeighbor (fst(head board)) sq then val `delete` snd(head board):delV val sq (tail board)
else (snd(head board)):delV val sq (tail board)
您有一些格式错误的代码,您想要的输出有一个 7
,看起来您想要一个 8
。我假设这就是你的意思:
import Data.List
delV val sq board =
if board == [] then []
else if isNeighbor (fst(head board)) sq
then val `delete` snd(head board):delV val sq (tail board)
else (snd(head board)):delV val sq (tail board)
哪里
delV :: Int -> Int -> Board -> [[Int]]
delV 5 2 [(2, [1,5,7]), (26, [1,5,8])] == [[1,7],[1,5,8]]
如你所愿
delV :: Int -> Int -> Board -> Board
delV 5 2 [(2, [1,5,7]), (26, [1,5,8])] == [(2, [1,7]), (26, [1,5,8])]
您只需将索引加回去即可。就是这样,继续你的风格:
delV :: Int -> Int -> Board -> Board
delV val sq board =
if board == [] then []
else if isNeighbor (fst(head board)) sq
then (fst(head board), val `delete` snd(head board)):
delV val sq (tail board)
else head board:delV val sq (tail board)
这里是地图方面的:
delV2 :: Int -> Int -> Board -> Board
delV2 value square = map f
where
f (index, candidates) | isNeighbor index square =
(index, delete value candidates)
f t = t