Skip to content

Instantly share code, notes, and snippets.

@bruno-cadorette
Created June 19, 2015 04:43
Show Gist options
  • Select an option

  • Save bruno-cadorette/2485c875b552beebd010 to your computer and use it in GitHub Desktop.

Select an option

Save bruno-cadorette/2485c875b552beebd010 to your computer and use it in GitHub Desktop.
Mask an ip address
import Control.Monad
import Data.Bits
import Data.List
import Data.List.Split
import Data.Word
createMask::Int -> [Word8]
createMask mask = take 4 $ gen mask ++ repeat 0
where
gen mask
|mask >= 8 = 255 : gen (mask - 8)
|mask < 0 || mask > 32 = error "The mask value must be between 0 and 32"
|otherwise = [shiftL 255 (8 - mask)]
getIp::String->[Word8]
getIp = map read . splitOn "."
main = forever $ do
putStrLn "Quel est votre ip?"
ip <- fmap getIp getLine
putStrLn "Quel est votre masque?"
mask <- fmap (createMask . read) getLine
putStrLn $ intercalate "." $ map show $ zipWith(.&.) mask ip
putStrLn "-------------------------------------------------"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment