[Haskell-cafe] Need feedback on my Haskell code

CK Kashyap ck_kashyap at yahoo.com
Wed Jul 29 06:04:55 EDT 2009

It worked like a charm!!! I'd need more time to get my head around "unfoldr"
I'd appreciate it very much if you could explain this line  "map maySwitch . unfoldr go $ (x1,y1,0)"
I did not fully understand the "$" in that line - I tried putting parenthesis in various places to get rid of "$" but did not seem to work.


From: Chaddaï Fouché <chaddai.fouche at gmail.com>
To: CK Kashyap <ck_kashyap at yahoo.com>
Cc: haskell-cafe at haskell.org
Sent: Tuesday, July 28, 2009 7:10:38 PM
Subject: Re: [Haskell-cafe] Need feedback on my Haskell code

On Tue, Jul 28, 2009 at 3:04 PM, CK Kashyap<ck_kashyap at yahoo.com> wrote:
> Hi Everyone,
> I managed to write up the line drawing function using the following links -
> http://www.cs.helsinki.fi/group/goa/mallinnus/lines/bresenh.html
> http://rosettacode.org/wiki/Bresenham%27s_line_algorithm#Haskell

I tried to simplify your function a little bit :

line :: Point -> Point -> [Point]
line pa@(xa,ya) pb@(xb,yb) = map maySwitch . unfoldr go $ (x1,y1,0)
    steep = abs (yb - ya) > abs (xb - xa)
    maySwitch = if steep then (\(x,y) -> (y,x)) else id
    [(x1,y1),(x2,y2)] = sort [maySwitch pa, maySwitch pb]
    deltax = x2 - x1
    deltay = abs (y2 - y1)
    ystep = if y1 < y2 then 1 else -1
    go (xTemp, yTemp, error)
        | xTemp > x2 = Nothing
        | otherwise  = Just ((xTemp, yTemp), (xTemp + 1, newY, newError))
          tempError = error + deltay
          (newY, newError) = if (2*tempError) >= deltax
                             then (yTemp+ystep,tempError-deltax)
                             else (yTemp,tempError)

I think it will be a bit better, tell me what you think ?


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090729/19712da3/attachment.html

More information about the Haskell-Cafe mailing list