I’m struggling with Haskell programming.
I’ve got the list below and I want to make it stack on each other, so it’d be like 3 x 4 pixel image. eg:

and how can I change the value of the first row or second …
eg: say like I want to make it darker or whiter (0 represents black and 255 represents white)
type Pixel = Int
type Row = [Pixel]
type PixelImage = [Row]
print :: PixelImage
print = [[208,152,240,29],[0,112,255,59],[76,185,0,152]]
The code I’ve got here does not stack the list and I don’t know how to stack it.
Please help, I’m really struggling with this.
Thanks in advance!
Use a name other than
printfor your image to avoid collisions with the Prelude’sprint.This is an inefficient representation, but it will do for a learning exercise.
You could print a
PixelImagewith the rows stacked on top of one another with a few imports at the top of your source and an I/O action:That may look intimidating, but everything there is familiar. The word order is just a little funny. For each
Rowin thePixelImage(forM_, a lot like aforloop in other languages) we print (withputStrLn) the list ofPixelvalues separated by two spaces (thanks tointercalate) and left-padded with spaces to make uniform 3-character fields (printf).With the image from your question, we get
Haskell lists are immutable: you cannot modify one in-place or destructively. Instead, think of it in terms of making a different list that’s identical to the original except for the specified row.
This gives your function a chance to fire for each
Rowin thePixelImage. Say you want to zero out a particular row:Reversing a row is
In another language, you might write
img[2] = [1,2,3,4], but in Haskell it’sThat usage isn’t terribly evocative, so we can defined
setRowin terms ofmodifyRow, a common technique in functional programming.Nicer:
Maybe you want to scale the pixel values instead.
For example:
Adding
scaleImageto apply a scaling factor to eachPixelin aPixelImagemeans a bit of refactoring to avoid repeating the same code in multiple places. We’d like to be able to useto get, say
This means
scaleOneRowshould bewhich promotes
clampto a toplevel function onPixelvalues.This in turn simplifies
scaleRow: