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.
Edited by cordedconch, 22 March 2010 - 09:04 PM.
Edited by cordedconch, 22 March 2010 - 09:04 PM.
|
|
|
greyPixel :: Pixel -> Pixel[FONT=monospace] [/FONT]greyPixel (r,g,b) = let a = (r+g+b) `div` 3 in (a, a, a)A where expression would also work.
greyscale :: Transform greyscale [] = [][SIZE=4] [/SIZE]grayscale (row:rows) = greyPixel greyRow row : greyPixel rowsThis code:
greyPixel greyRow rowdoes 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:
greyscale (row:rows) = greyRow row : greyscale rowsAlso 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:
greyRow [] = [] greyRow (pixel:pixels) = greyPixel pixel : [B]greyRow[/B] pixelsHowever, 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.
greyscale :: Transform greyscale = map (map (\(r,g,b) -> let a = (r+g+b) `div` 3 in (a,a,a)))Done.
Edited by ZekeDragon, 20 March 2010 - 11:35 AM.