[Haskell-cafe] converting prefixes of CString <-> String

Malcolm Wallace malcolm.wallace at me.com
Tue Apr 26 15:41:48 CEST 2011


On 26 Apr 2011, at 13:31, Eric Stansifer wrote:

>>> Let 'c2h' convert CStrings to Haskell Strings, and 'h2c' convert
>>> Haskell Strings to CStrings.  (If I understand correctly, c2h . h2c
>>> === id, but h2c . c2h is not the identity on all inputs;
>> 
>> That is correct.  CStrings are 8-bits, and Haskell Strings are 32-bits.  Converting from Haskell to C loses information, unless you use a multi-byte encoding on the C side (for instance, UTF8).
> 
> So actually I am incorrect, and h2c . c2h is the identity but c2h . h2c is not?

Ah, my bad.  In reading the composition from right to left, I inadvertently read h2c and c2h from right to left as well!  So, starting from C, converting to Haskell, and back to C is the identity, yes.  Starting from Haskell, no.

Regards,
    Malcolm



More information about the Haskell-Cafe mailing list