If statement 当条件为True时,将特定元素添加到列表中

If statement 当条件为True时,将特定元素添加到列表中,if-statement,haskell,binary,If Statement,Haskell,Binary,如果在8位长的数字(例如146)中设置了位,我想添加一个元组 我的代码如下所示,Haskell只是打印到第一个真正的表达式: returnPossibleMoves stone = if testBit (look stone) 0 then [(0,-1)] else [(0,0)] ++ if testBit (look stone) 1 then [(1,-1)] else [(0,0)] ++

如果在8位长的数字(例如146)中设置了位,我想添加一个元组

我的代码如下所示,Haskell只是打印到第一个真正的表达式:

returnPossibleMoves stone = if testBit (look stone) 0 then [(0,-1)] else [(0,0)] ++
                            if testBit (look stone) 1 then [(1,-1)] else [(0,0)] ++
                            if testBit (look stone) 2 then [(1,0)] else [(0,0)] ++
                            if testBit (look stone) 3 then [(1,1)] else [(0,0)] ++
                            if testBit (look stone) 4 then [(0,1)] else [(0,0)] ++
                            if testBit (look stone) 5 then [(-1,1)] else [(0,0)] ++
                            if testBit (look stone) 6 then [(-1,0)] else [(0,0)] ++
                            if testBit (look stone) 7 then [(-1,-1)] else [(0,0)]
使用look stone=146->10010

所以我的回报只是:
[(0,0)、(1,-1)]


还有可能去掉其他的吗?

您可以通过在此处添加括号来修复代码。请注意,
else
案例应该只产生一个空列表。例如:

returnPossibleMoves stone =
    (if testBit (look stone) 0 then [(0,-1)] else []) ++
    (if testBit (look stone) 1 then [(1,-1)] else []) ++
    (if testBit (look stone) 2 then [(1,0)] else []) ++
    (if testBit (look stone) 3 then [(1,1)] else []) ++
    (if testBit (look stone) 4 then [(0,1)] else []) ++
    (if testBit (look stone) 5 then [(-1,1)] else []) ++
    (if testBit (look stone) 6 then [(-1,0)] else []) ++
    (if testBit (look stone) 7 then [(-1,-1)] else [])
returnPossibleMoves stone = map snd (filter (testBit (look stone) . fst) (zip [0..] moves))
    where moves = [(0,-1), (1,-1), (1,0), (1,1), (0,1), (-1,1), (-1,0), (-1,-1)]
intSin :: Int -> Int
intSin x
    | x .&. 3 == 0 = 0
    | otherwise = 1 - shiftR (x .&. 4) 1
或具有列表理解能力,如:

(如果我们将
look
设置为
id


这是有意义的,因为对于
146
设置了第二位、第五位和第八位,因此我们返回第二位(
(1,-1)
)、第五位(
(0,1)
)和第八位(
(-1,-1)
)元素。

您可以通过在此处添加括号来修复代码。请注意,
else
案例应该只产生一个空列表。例如:

returnPossibleMoves stone =
    (if testBit (look stone) 0 then [(0,-1)] else []) ++
    (if testBit (look stone) 1 then [(1,-1)] else []) ++
    (if testBit (look stone) 2 then [(1,0)] else []) ++
    (if testBit (look stone) 3 then [(1,1)] else []) ++
    (if testBit (look stone) 4 then [(0,1)] else []) ++
    (if testBit (look stone) 5 then [(-1,1)] else []) ++
    (if testBit (look stone) 6 then [(-1,0)] else []) ++
    (if testBit (look stone) 7 then [(-1,-1)] else [])
returnPossibleMoves stone = map snd (filter (testBit (look stone) . fst) (zip [0..] moves))
    where moves = [(0,-1), (1,-1), (1,0), (1,1), (0,1), (-1,1), (-1,0), (-1,-1)]
intSin :: Int -> Int
intSin x
    | x .&. 3 == 0 = 0
    | otherwise = 1 - shiftR (x .&. 4) 1
或具有列表理解能力,如:

(如果我们将
look
设置为
id


这是有意义的,因为对于
146
设置了第二位、第五位和第八位,因此我们返回第二位(
(1,-1)
)、第五位(
(0,1)
)和第八位(
(-1,-1)
)元素。

只是为了好玩,这里有一个昂贵但很漂亮的方法:

intSin :: Int -> Int
intSin n = round (sin (pi*fromIntegral n/4))

returnPossibleMoves stone =
    [ (intSin i, intSin (i-2))
    | i <- [0..7]
    , testBit (look stone) i
    ]

只是为了好玩,这里有一个昂贵但美丽的方式:

intSin :: Int -> Int
intSin n = round (sin (pi*fromIntegral n/4))

returnPossibleMoves stone =
    [ (intSin i, intSin (i-2))
    | i <- [0..7]
    , testBit (look stone) i
    ]

次要备选方案:
[move |(i,move)次要备选方案:
[move |(i,move)可能是XY问题。考虑到Haskell中可用的富类型系统,可能有比将
Int
视为位数组更好的信息编码方式。可能是XY问题。考虑到Haskell中可用的富类型系统,可能有比将
Int
视为位更好的信息编码方式大堆