Though it is somewhat academic, I really like the example that Abelson and Sussman use in section 1.3.1 to explain the “procedures as arguments” scenario. To illustrate this concept they implement summation using Scheme. They implement a higher-order procedure called “sum” that takes two procedures and a range of numbers as arguments. One procedure is used for computing the value that is added to the sum and the other is used to determine the next value to compute. The Scheme code can be seen here.
It’s not as useful as map and reduce, but those are not discussed until Chapter 2.
As some of you may know C# 3.0 adds a whole slew of functional/dynamic features. Whether this is an effort to keep up with the cool kids or merely a necessity for making LINQ work is not important to me. What I’m curious to know is if the features make it easier to implement the concepts in SICP using C# 3.0.
Implementing “Sum” in C# 3.0
Following along with the example from SICP - let’s say that you have defined the following methods.
Common to each method is the idea of iterating over a range of numbers and adding the result of each computation to the total. If we create a higher-order procedure called “Sum” to abstract out this behavior we can then redefine each method.
I had to cheat and do some casting shenanigans because the division operation in "PiSum()" brings floating point numbers into the mix, but I feel like this code does capture the essence of the original Scheme implementation. Implementing "Sum" as a generic method might be a way to make the code more elegant.
If subverting the type system does not appeal to you, the following JavaScript implementation might be of interest.
It turns out that there are other people interested in implementing SICP in different languages. Chris Rathman has translated parts of SICP into 19 other languages. One disturbing thing to note about Chris’s C# translation is that it doesn’t go beyond chapter 1. Does that mean that it is not possible to implement the code from the rest of the book in C#?
dasBlog theme by Mads Kristensen
I'm a programmer and consultant specializing in .NET development. I've worked in several industries including bio-tech, healthcare, and financial services. When not coding or learning about new technologies, I enjoy running, playing video games, and watching New York Giants football.
RSS Feed | E-mail
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.