Closed Thread
Results 1 to 4 of 4

Thread: Specific Haskell recursion question

  1. #1
    cordedconch is offline Newbie
    Join Date
    Mar 2010
    Location
    Providence, RI
    Posts
    3
    Rep Power
    0

    Talking Specific Haskell recursion question

    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.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: Specific Haskell recursion question

    I'd use a let expression in greyPixel:
    Code:
    greyPixel :: Pixel -> Pixel
    greyPixel (r,g,b) = let a = (r+g+b) `div` 3 in (a, a, a)
    A where expression would also work.

    I think your problem (if it's a build problem, that is) is in the greyscale function:
    Code:
    greyscale :: Transform
    greyscale  []         = []
    grayscale  (row:rows) =  greyPixel greyRow row : greyPixel rows
    This code:
    Code:
    greyPixel greyRow row
    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:
    greyscale  (row:rows) = greyRow row : greyscale rows
    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:
    greyRow []             = []
    greyRow (pixel:pixels) = greyPixel pixel : greyRow pixels
    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:
    greyscale :: Transform
    greyscale = map (map (\(r,g,b) -> let a = (r+g+b) `div` 3 in (a,a,a)))
    Done.
    Last edited by ZekeDragon; 03-20-2010 at 12:35 PM.
    Wow I changed my sig!

  4. #3
    ziwei is offline Newbie
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Re: Specific Haskell recursion question

    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?

  5. #4
    mings is offline Newbie
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Re: Specific Haskell recursion question

    thank you for your forum.It's helpful for me.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Haskell Question
    By cordedconch in forum General Programming
    Replies: 0
    Last Post: 06-11-2010, 03:55 AM
  2. Question About Learning C# For Very Specific Usage
    By Mulsiphix in forum C# Programming
    Replies: 5
    Last Post: 12-28-2009, 09:01 AM
  3. Haskell question
    By CChris in forum General Programming
    Replies: 8
    Last Post: 10-20-2009, 09:26 AM
  4. Haskell - quick question
    By a-z-z in forum General Programming
    Replies: 0
    Last Post: 11-02-2008, 08:07 AM
  5. Specific Coding Question
    By nassersyed in forum Visual Basic Programming
    Replies: 5
    Last Post: 11-01-2007, 11:20 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts