#!/usr/bin/envstack{- stack
--resolver lts-12.0
--install-ghc
script
--
--ghc-options -Werror
--ghc-options -Wall
-}--------------------------------------------------------------------------------{-# LANGUAGE OverloadedStrings #-}{-# LANGUAGE Safe #-}--------------------------------------------------------------------------------moduleMain(main)where--------------------------------------------------------------------------------importData.Bits(Bits,shiftL,shiftR)importqualifiedData.ByteString.Lazy.Char8asL8importData.Word(Word16,Word32,Word64,Word8)--------------------------------------------------------------------------------(.<.)::Bitsa=>a->Int->a(.>.)::Bitsa=>a->Int->acons::Char->L8.ByteString->L8.ByteStringtoBase::Int->(Integer->Integer)->Integer->L8.ByteStringtoBit::Integer->L8.ByteStringtoHex'::Bool->Integer->L8.ByteStringtoHex::Integer->L8.ByteStringtoHexLow::Integer->L8.ByteStringmain::IO()--------------------------------------------------------------------------------(.<.)xy=x`shiftL`y(.>.)xy=x`shiftR`y-- O(1) cons is analogous to '(:)' for lists.cons=L8.constoBasebasef=aux""whereauxacc0=accauxaccn=aux(consracc)cwherec=n.>.baser=toEnum.fromIntegral$f(n-c*1.<.base)toBit=toBase1{- 2^1 = 002 -}(+48)toHex'cap=toBase4{- 2^4 = 016 -}auxwhereauxx|x<0x0A=48+x|x<0x10=cc+x|otherwise=error"Shouldn't be possible to reach (toHex')"cc=ifcapthen55else87toHex=toHex'TruetoHexLow=toHex'FalsetoByte=toBase8{- 2^8 = 256 -}id--------------------------------------------------------------------------------main=doL8.putStrLn"-- Word08 as bits:"L8.putStrLn$toBit$fromIntegral$w08L8.putStrLn"-- Word08 as hex:"L8.putStrLn$toHex$fromIntegral$w08L8.putStrLn"-- Word08 as bytes:"L8.putStrLn$toByte$fromIntegral$w08L8.putStrLn$""L8.putStrLn"-- Word16 as bits:"L8.putStrLn$toBit$fromIntegral$w16L8.putStrLn"-- Word16 as hex:"L8.putStrLn$toHex$fromIntegral$w16L8.putStrLn"-- Word16 as bytes:"L8.putStrLn$toByte$fromIntegral$w16L8.putStrLn$""L8.putStrLn"-- Word32 as bits:"L8.putStrLn$toBit$fromIntegral$w32L8.putStrLn"-- Word32 as hex:"L8.putStrLn$toHex$fromIntegral$w32L8.putStrLn"-- Word32 as bytes:"L8.putStrLn$toByte$fromIntegral$w32L8.putStrLn$""L8.putStrLn"-- Word64 as bits:"L8.putStrLn$toBit$fromIntegral$w64L8.putStrLn"-- Word64 as hex:"L8.putStrLn$toHex$fromIntegral$w64L8.putStrLn"-- Word64 as bytes:"L8.putStrLn$toByte$fromIntegral$w64L8.putStrLn$""L8.putStrLn"-- Integer as bits:"L8.putStrLn$toBit$intL8.putStrLn"-- Integer as hex:"L8.putStrLn$toHex$intL8.putStrLn"-- Integer as bytes:"L8.putStrLn$toByte$fromIntegral$intwherew08=0xFF::Word8-- 1 .<. 08 - 1w16=0xFFFF::Word16-- 1 .<. 16 - 1w32=0xFFFFFFFF::Word32-- 1 .<. 32 - 1w64=0xFFFFFFFFFFFFFFFF::Word64-- 1 .<. 64 - 1int=0x10000000000000000::Integer-- 1 .<. 64