一种解决方案是仅折叠列表:
import Data.Bits (Bits, shiftL, (.|.))
pack :: (Num b, Foldable t, Bits b) => t b -> b
pack a = foldl go 0 a
where go acc i = (acc `shiftL` 1) .|. i
Run Code Online (Sandbox Code Playgroud)
你会得到:
\> pack [0,1,0,0,0,1,1,1,1,0]
286
Run Code Online (Sandbox Code Playgroud)