[Haskell-beginners] clarity and complexity of Haskell code?
Chaddaï Fouché
chaddai.fouche at gmail.com
Wed Sep 28 22:20:26 CEST 2011
On Sun, Sep 25, 2011 at 10:05 PM, Ozgur Akgun <ozgurakgun at gmail.com> wrote:
> Hi.
>
> On 25 September 2011 18:10, Brent Yorgey <byorgey at seas.upenn.edu> wrote:
>>
>> You must at least agree that it is short.
>
> Not trying to start language wars here, but it is not terribly short for
> what it does. The following code does the same thing in C#, and isn't far
> longer. And it has more or less a one-to-one correspondence to the given
> Haskell code; open a file for reading, open a file for writing, read some
> number of bytes, apply the transformation, write it to the output file.
> Flushing the input/output buffers and closing the files are handled by the
> using construct, similar to withFile in the Haskell example.
> int chunksize = 4096;
> using (var r = new BinaryReader(File.OpenRead("infile")))
> using (var w = new BinaryWriter(File.OpenWrite("outfile")))
> for (var buffer = r.ReadBytes(chunksize); buffer.Length > 0; buffer
> = r.ReadBytes(chunksize))
> w.Write(Array.ConvertAll(buffer, p => (byte) ~p));
Note that this code is pretty close to FP already (except the "for"
loop which is where the iteratees/enumerator that present the main
difficulty in Haskell trying to do the FP equivalent intervene) : the
"using" is pretty declarative, you use a closure and a higher order
function...
--
Jedaï
More information about the Beginners
mailing list