Comments by "muh diversity" (@muhdiversity7409) on "Lecture 1A: Overview and Introduction to Lisp" video.
-
Thank you for making me want to learn Lisp after decades of actively avoiding it. I was a little confused in the square root example until I grokked that there was recursion. To test my understanding of the material I implemented the same module in C++ using named lamdas and a slow hack that allows recursion in C++ lamba expressions. "try" is a reserved c++ word so I replaced it with tryme(). I think the C++ version of this would be even more unreadable without the auto names.
double lisp_sqrt2(double s){
std::function<double(double,double)> tryme;
tryme = [&tryme](double guess,double root)->double {
auto average = [](double a,double b)->double {return (a + b) / 2.0;};
auto improve = [average](double guess,double root)->double {return average(guess,root/guess);};
auto goodenough = [](double guess,double root)->bool {return abs(root - (guess * guess)) < 0.000001;};
if (goodenough(guess,root))
return guess;
return tryme(improve(guess,root),root);
};
return tryme(1,s);
}
6