[Haskell-cafe] Need feedback on my Haskell code

david48 dav.vire+haskell at gmail.com
Wed Jul 29 07:13:52 EDT 2009


On Wed, Jul 29, 2009 at 12:04 PM, CK Kashyap<ck_kashyap at yahoo.com> wrote:
> map maySwitch . unfoldr go $ (x1,y1,0)

I'm not an expert and I might say things the wrong way or without the
required rigor, so with this disclaimer here's my explanation :

go calculates a step of the line, given the current coordinates and
the error value
it returns nothing if the line is done.

unfoldr go calculates a list of lines coordinates, keeping calling go,
and stopping when go returns nothing.

maySwitch takes a coordinate, and switches the x and y values
depending on the axis we're following
map maySwitch does the same for the entire list of coordinates.

when you compose the two,
map maySwitch . unfoldr go  is then a function that takes initial
coordinates, makes a list of coordinates and may switch the x's and
y's depending on the axis we're following.


Now (.) takes two functions, namely map maySwitch and unfoldr go. If
you don't write the $, what you actually mean is
(map maySwitch) . ( unfoldr go (x1,y1,0))

this ( unfoldr go (x1,y1,0)) is not of the right type for (.) : it
should take a parameter and return a value, but here it just returns a
value.

so you have to find a way to give (x1,y1,0) to the whole composed
function  map maySwitch . unfoldr go.

the obvious way to do it is by writing:

( map maySwitch . unfoldr go ) (x1,y1,0 )

the $ is just a more readable way to write it : since $ binds with
less priority, in
map maySwitch . unfoldr go $ (x1,y1,0)
what's on the right of $ will be applied to what's on the left

David.


More information about the Haskell-Cafe mailing list