Yes this way of generating the list is lazy. In general, list functions in Haskell are lazy.
You can test whether it's lazy yourself by including something that will error out (e.g. undefined
) in the initial list. For example, if
vertexes = [(0,0), (10,0), undefined, undefined]
then vertexPairs
will give you an error (since it needs to evaluate the whole list to print it). However, if it's lazy, head vertexPairs
should still give you the correct pair--and it does!
I think your code actually looks rather good. The tail vertexes ++ [head vertex]
makes what you're doing very clear. Yes, it looks a little odd to use ++
here, but it makes sense: appending to the end of the list is an expensive operation, so it should stand out. I can't think of any better way to write that code. As a minor style hint, you can drop the parentheses around vertexes
:
vertexPairs = zip vertexes (tail vertexes ++ [head vertexes])
For 3., conceptually, you want to apply isInside point
to each pair. Right now it has a type like Point -> Point -> Bool
. You want to get a function that takes its first two arguments as a tuple: (Point, Point) -> Bool
. This function is called uncurry
because the opposite transformation (turning a function that expects a tuple into one of multiple parameters) is called currying. So you could write 3. like this:
results = map (uncurry (isInside point)) vertexPairs