Another fun tidbit from going through SICP.
Consider the problem of representing line segments in a plane. Each segment is represented as a pair of points: a starting point and an ending point. Define a constructor make-segment and selectors start-segment and end-segment that define the representation of segments in terms of points. Furthermore, a point can be represented as a pair of numbers: the x coordinate and the y coordinate. Accordingly, specify a constructor make-point and selectors x-point and y-point that define this representation. Finally, using your selectors and constructors, define a procedure midpoint-segment that takes a line segment as argument and returns its midpoint (the point whose coordinates are the average of the coordinates of the endpoints).
My first approach was to use the constructors they described, modernizing the data structures slightly to make it easier to understand.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
With the data structures make-point and make-segment aren’t incredibly useful. I won’t reference them again, but I ended up just defining the data structures directly and deleting the constructors.
The first duplication I eliminated was between the two averages, as the only thing that changed was the axis.
1 2 3 4 5 6
Better, but I’m not sure things got easier to read, and there’s still that duplicated structure between extracting the start and end points.
Something that will make this easier to understand is naming the averaging concept with a function.
1 2 3 4 5 6 7 8 9
That’s a little easier to understand, but still working on a pretty low level. One thing about clojure is there are so few data types that often there’s a higher level concept provided by the language.
1 2 3
I was amazed at how clear and expressive the final representation was. The intermediate refactorings helped me see what I was trying to do from a higher level, and this ultimately gave me the insight that I was actually merging the points together with an average.