<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:times new roman,new york,times,serif;font-size:12pt"><div>Hi,<br>I had posted a note on line drawing algo with Haskell some time back. Now, I am trying to write a PNM image.<br><br>import qualified Data.ByteString as B<br><br>width = 256<br>height = 256<br>bytesInImage = width * height * 3<br>blankImage =&nbsp; B.pack $ take bytesInImage (repeat 0)<br><br>type Color = (Int,Int,Int)<br>setPixel :: B.ByteString -&gt; Int -&gt; Int -&gt; Color -&gt; B.ByteString<br>setPixel image x y (r,g,b) = B.concat [beforePixel, pixel, afterPixel]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; beforePixel = B.take before image<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; afterPixel = B.drop (before+3)
 image<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pixel=B.pack [(fromIntegral r),(fromIntegral g),(fromIntegral b)]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- number of bytes before the 3 bytes of<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- the pixel at x y<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; before = (y * width * 3) + (x * 3) - 3<br><br>main = do<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putStrLn "P6"<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putStrLn ( (show width) ++ " " ++ (show height) )<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putStrLn "255"<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- Set a red pixel at 100 100<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; B.putStr (setPixel blankImage 100 100 (255,0,0))
 <br><br><br>Can I please have some review comments on the code above? Would recreating the entire ByteString for each setPixel be an overhead?<br>Also, I am barely beginning to grasp the Monad concept....I was wondering if there could be a monadic style of implementation of this - that could potentially have a series of setPixels inside a do block?<br><br>Regards,<br>Kashyap<br></div></div><br>

      </body></html>