[Haskell-cafe] Re: Should inet_ntoa Be Pure?

Dominic Steinitz dominic.steinitz at blueyonder.co.uk
Sat May 14 08:34:28 EDT 2005


On Monday 09 May 2005 11:45 am, you wrote:
> On 07 May 2005 11:59, Dominic Steinitz wrote:
> > Does anyone know why these are in the IO monad? Aren't they pure
> > functions converting between dotted-decimal strings and a 32-bit
> > network byte ordered binary value?
> >
> > Dominic.
>
> http://www.haskell.org/ghc/docs/latest/html/libraries/network/Network.So
> cket.html#v%3Ainet_addr
>
> http://www.haskell.org/ghc/docs/latest/html/libraries/network/Network.So
> cket.html#v%3Ainet_ntoa
>
> Clearly it should be pure.  To avoid breaking the interface, we could
> add:
>
>   showHostAddress :: HostAddress -> String
>
> and deprecate inet_ntoa?
>
> Cheers,
> 	Simon
I haven't followed the thread on this carefully but as a pragmatic approach, 
how about:

hostAddress :: HostAddress -> (Word8,Word8,Word8,Word8)
inetAddress :: (Word8,Word8,Word8,Word8) -> HostAddress

then there shouldn't be any errors to deal with as all the addresses are valid 
in both representations. And if you want to show the address you can always 
do:

showHostAddress x =
   concat $ intersperse "." (map show [first x, second x, third x, fourth x])
      where
         first (a,_,_,_) = a
         second (_,b,_,_) = b
         third (_,_,c,_) = c
         fourth (_,_,_,d) = d

Dominic.



More information about the Haskell-Cafe mailing list