title: Converting an Int to a list of bits represented by [Bool]
body: <p>I'm aware that with <a href="https://hackage.haskell.org/package/base-4.15.0.0/docs/Data-Bits.html" rel="nofollow noreferrer"><code>Data.Bits</code></a> I can do bitwise manipulation on anything with an instance of <code>Bits</code>. That however is not what I want. I explicitly want to manipulate a list of <code>Bool</code>s which represent the bit value at each bit index.</p>
  <p>More precisely I want to manipulate the bits of a 1-byte integer.</p>
  <p>I have managed to get this to work using <code>map</code>/<code>testBit</code> and <code>foldl'</code>/<code>setBit</code>. Which you can see from my tests in ghci below:</p>
  <pre><code>λ import Data.Bits
  λ import Data.List
  
  λ let convertToBits :: Int -&gt; [Bool] ; convertToBits x = map (testBit x) [7,6..0]
  
  λ let convertFromBits :: [Bool] -&gt; Int ; 
     convertFromBits bs = foldl' (\acc (i,b) -&gt; if b then setBit acc i else acc) 0 $ zip [7,6..0] bs
  
  λ convertToBits 255
  [True,True,True,True,True,True,True,True]
  
  λ convertToBits 2
  [False,False,False,False,False,False,True,False]
  
  λ convertFromBits [False,False,False,True,True,False,False,True]
  25
  
  λ convertFromBits $ convertToBits 255
  255
  
  λ convertFromBits $ convertToBits 10
  10
  </code></pre>
  <p>However this doesn't feel like a particularly great implementation. I'm wondering if this (or something similar) might be in standard library?</p>
  <p>EDIT ------</p>
  <p>A quick final note, even though I have answered the question below, I thought I'd add a bit more in case anyone stumbles on this and knows a better answer. I'll be storing all of the bits in a <code>UArray</code> which will bit-pack the <code>Bool</code> values.</p>
  <p>In thinking about it, what would be really good is a function that converts a <code>Word8</code> into a <code>UArray ix Bool</code>. I'd like to think there is some efficient / simple way to do this but I don't know enough about the low level stuff to work it out.</p>