I'm trying to get a haskell program to work.
Basically my program performs basic operations.
I'm trying to write the Grey Scale function.
It would be awesome if someone could help steer me on the right track with these recursion functions.
Last edited by cordedconch; 03-22-2010 at 10:04 PM.
I'd use a let expression in greyPixel:
A where expression would also work.Code:greyPixel :: Pixel -> Pixel greyPixel (r,g,b) = let a = (r+g+b) `div` 3 in (a, a, a)
I think your problem (if it's a build problem, that is) is in the greyscale function:
This code:Code:greyscale :: Transform greyscale [] = [] grayscale (row:rows) = greyPixel greyRow row : greyPixel rows
does NOT run greyRow on row then pass the results to greyPixel, this runs greyPixel with two parameters, greyRow and row. This is obviously not what you want, further it won't even work even if you scoped it properly. You should eliminate "greyPixel" from that, also you're inconsistent with your spelling of 'grey', which in this function is spelled both 'grey' and 'gray'. It should look like this:Code:greyPixel greyRow row
Also your greyRow function isn't recursing properly, again to perform recursion you're supposed to define the function in terms of itself. Like this for greyRow:Code:greyscale (row:rows) = greyRow row : greyscale rows
However, in all these cases I wouldn't even bother using recursion. It's considered idiomatic in the Haskell community to not use direct recursion whenever possible, so unless your assignment directly mandates it I'd just use the map expression.Code:greyRow [] = [] greyRow (pixel:pixels) = greyPixel pixel : greyRow pixels
Done.Code:greyscale :: Transform greyscale = map (map (\(r,g,b) -> let a = (r+g+b) `div` 3 in (a,a,a)))
Last edited by ZekeDragon; 03-20-2010 at 12:35 PM.
Wow I changed my sig!
Hi, I wrote the greyscale program and it doesn't work, so I tried and figure out what's wrong. I am so luck to find your reply. However, after I compare what my modules and yours, I found they are exactly the same. But it just doesn't work!!I really got despaired. Could you help to find out what's wrong with my program?
thank you for your forum.It's helpful for me.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks