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 <[email protected]>
To: CK Kashyap <[email protected]>
Cc: [email protected]
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 <[email protected]> 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


      
_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to