Fun with Haskell

Haskell is a great programming language and I have a lot of fun programming in it. I've been working fiddling around with a simple prototype for a board-game style game (more details on that later) and was figuring out how to draw a line connecting some positions on the game board. Specifically, I needed to figure out how to go from a calculated path to a list of line segments to draw. So, turning a list like this, where each element is a coordinate pair:
[(1,1), (2,3), (4,4)]
Into a list like this, where each element is a line start/end pair:
[((1,1),(2,3)), ((2,3),(4,4))]
This is a super easy problem to solve in any imperative language, just iterate through the elements making a connecting list, but Haskell is fun because you get to come up with awesome one-line solutions that don't have any iteration:

zip x (tail x)

where x is your list. Done. Hooray!

Comments are closed.

Dev Journal and Project Hosting