[Haskell-cafe] Writing a pnm file

CK Kashyap ck_kashyap at yahoo.com
Mon Aug 3 06:17:27 EDT 2009


Thanks Sebastian,
Array/accumArray sounds like what I am looking for.

Int -> ( a -> a ) -> [a] -> [a]
approach, would it not be expensive on memory as well? Or is it just speed?

Regards,
Kashyap




________________________________
From: Sebastian Sylvan <sebastian.sylvan at gmail.com>
To: CK Kashyap <ck_kashyap at yahoo.com>
Cc: haskell-cafe at haskell.org
Sent: Monday, August 3, 2009 3:14:47 PM
Subject: Re: [Haskell-cafe] Writing a pnm file




On Mon, Aug 3, 2009 at 6:38 AM, CK Kashyap <ck_kashyap at yahoo.com> wrote:

Thanks Sebastian,
>ppm module is indeed very useful. So, I guess my question then just boils down to, how can I write a function to mimic the setPixel function ->
>
>Basically, a blank white image would look like this  (as per ppm module)
>[ 
>   [ (255, 255, 255)  , (255, 255, 255) , (255, 255, 255) ] ,  -- 3 columns of row 1
>   [ (255, 255, 255) , (255, 255, 255) , (255, 255, 255)  ]    --- 3 columns of row 2
>>]
>
>setPixel x y r g b when called like this - setPixel 0,0,255,0,0
>
>[ 
>   [ (255, 0, 0)  , (255, 255, 255) , (255, 255, 255) ] ,  -- 3 columns of row 1
>   [ (255, 255, 255) , (255, 255, 255) , (255, 255, 255)  ]    --- 3 columns of row 2
>>]
>
>What would be a good way to implement such a function?
>
 
Well you could start by writing a function like:
 
adjustElem :: Int -> ( a -> a ) -> [a] -> [a]
 
That would basically apply a function to a specific element in a list (indexed by the first parameter). Look at splitAt in Data.List, it may be useful. 
Then you can use this in a nested way, by calling adjustElem to modify the row you're interested in, and the function you pass in to adjust that row would in turn call adjustElem on the specific pixel in that row).
 
However, this may be very slow. If you don't care about speed it'll work fine, but if you really do want to build up an image by successive single-pixel modifications, you should consider first using an Array and accumArray, this will be much faster as internally accumArray can use a mutable array (while the external interface is still pure).
 
 
Sebastian


      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090803/45c3fe07/attachment-0001.html


More information about the Haskell-Cafe mailing list