Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 997159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:59:19+00:00 2026-05-16T06:59:19+00:00

I’m struggling with Haskell programming. I’ve got the list below and I want to

  • 0

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:

pixel image

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:59:20+00:00Added an answer on May 16, 2026 at 6:59 am

    Use a name other than print for your image to avoid collisions with the Prelude’s print.

    type Pixel = Int
    type Row = [Pixel]
    type PixelImage = [Row]
    
    img :: PixelImage 
    img = [[208,152,240,29],[0,112,255,59],[76,185,0,152]]
    

    This is an inefficient representation, but it will do for a learning exercise.

    You could print a PixelImage with the rows stacked on top of one another with a few imports at the top of your source and an I/O action:

    import Control.Monad
    import Data.List
    import Text.Printf
    
    printImage :: PixelImage -> IO ()
    printImage img =
      forM_ img $ putStrLn . intercalate "  " . map (printf "%3d")
    

    That may look intimidating, but everything there is familiar. The word order is just a little funny. For each Row in the PixelImage (forM_, a lot like a for loop in other languages) we print (with putStrLn) the list of Pixel values separated by two spaces (thanks to intercalate) and left-padded with spaces to make uniform 3-character fields (printf).

    With the image from your question, we get

    ghci> printImage img
    208  152  240   29
      0  112  255   59
     76  185    0  152

    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.

    modifyRow :: PixelImage -> (Row -> Row) -> Int -> PixelImage
    modifyRow img f i = map go (zip [0..] img)
      where go (j,r) | i == j    = f r
                     | otherwise = r
    

    This gives your function a chance to fire for each Row in the PixelImage. Say you want to zero out a particular row:

    ghci> printImage $ modifyRow img (map $ const 0) 0
      0    0    0    0
      0  112  255   59
     76  185    0  152

    Reversing a row is

    ghci> printImage $ modifyRow img reverse 0
     29  240  152  208
      0  112  255   59
     76  185    0  152

    In another language, you might write img[2] = [1,2,3,4], but in Haskell it’s

    ghci> modifyRow img (const [1..4]) 2
    [[208,152,240,29],[0,112,255,59],[1,2,3,4]]

    That usage isn’t terribly evocative, so we can defined setRow in terms of modifyRow, a common technique in functional programming.

    setRow :: PixelImage -> Row -> Int -> PixelImage
    setRow img r i = modifyRow img (const r) i
    

    Nicer:

    ghci> printImage $ setRow img [4,3,2,1] 1
    208  152  240   29
      4    3    2    1
     76  185    0  152

    Maybe you want to scale the pixel values instead.

    scaleRow :: (RealFrac a) => PixelImage -> a -> Int -> PixelImage
    scaleRow img x i = modifyRow img f i
      where f = let clamp z | z < 0     = 0
                            | z > 255   = 255
                            | otherwise = truncate z
                  in map (clamp . (x *) . fromIntegral)
    

    For example:

    ghci> printImage $ scaleRow img 0.5 1
    208  152  240   29
      0   56  127   29
     76  185    0  152

    Adding scaleImage to apply a scaling factor to each Pixel in a PixelImage means a bit of refactoring to avoid repeating the same code in multiple places. We’d like to be able to use

    scaleImage :: (RealFrac a) => a -> PixelImage -> PixelImage
    scaleImage x = map $ scaleOneRow x
    

    to get, say

    ghci> printImage $ scaleImage 3 img 
    255  255  255   87
      0  255  255  177
    228  255    0  255

    This means scaleOneRow should be

    scaleOneRow :: (RealFrac a) => a -> Row -> Row
    scaleOneRow x = map (clamp . (x *) . fromIntegral)
    

    which promotes clamp to a toplevel function on Pixel values.

    clamp :: (RealFrac a) => a -> Pixel
    clamp z | z < 0     = 0
            | z > 255   = 255
            | otherwise = truncate z
    

    This in turn simplifies scaleRow:

    scaleRow :: (RealFrac a) => PixelImage -> a -> Int -> PixelImage
    scaleRow img x i = modifyRow img (scaleOneRow x) i
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.