dimanche 9 février 2020

Add specific element to List when Condition is True

I want to add a tuple if a bit is set in a 8 bit long number (e.g 146).

My code looks like this and Haskell is just printing to the first true expression:

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)]

with look stone = 146 -> 10010010

So my return is just: [(0,0),(1,-1)]

Also is it possible to get rid of the else?

Aucun commentaire:

Enregistrer un commentaire