403 points · by kamaal · 8y ago · 233 comments
The Abelson from the OP's lecture is the co-author, and the presentation described exactly follows the structure of the book (including the derivative example).
It opened my eyes to a new way of thinking about coding when I first read it (and worked through the exercises) many years ago.
Perhaps pragmatism aside, would you?
[1] thoroughly explains why
[2,3] based on How To Design Programs [4] is a much better intro; you should read SICP after completing this
[1] https://www2.ccs.neu.edu/racket/pubs/jfp2004-fffk.pdf
[2] https://www.edx.org/course/how-code-simple-data-ubcx-htc1x
[3] https://www.edx.org/course/how-code-complex-data-ubcx-htc2x
I don't think its fair to continue the fiction that introduction to programming 101 at MIT is about pitching to people who have never done programming before any more than it is to pretend that people who get into top medical or law schools haven't had multiple years being coached to get into top medical or law schools.
Humbly, its not a beginner programming book in any reality-connected universe...
Self-study SICP without any previous programming experience would be exceedingly tough.
Many of the illustrations are based on mathematics and a lot of problems require a decent amount of mathematical maturity to tackle. I think it’s a great book for anyone decently mathematically competent but for people who don’t really know what to write when asked for a proof, I think it’s a lot more difficult.
Apart from the mathematics based examples, a lot of the way things are explained is not very related to actual machines and relies a lot on abstract symbol manipulation, something that is easy not to notice if one is used to mathematics, but is often very difficult if one is not.
I disagree strongly on that assertion. The mathematics in SICP was what made it stand out for me, and what got me into programming. Other books I read before that just presented programming as an opaque thing where you follow instructions and write incantations without any rhyme or reason.
In general terms, the math-phobia that has been prevalent in computer programming for the past 20 years is a new phenomenon (I have not seen it in literature from the 1990s or earlier, or heard it from old-school programmers and CS people) and is at its core deeply anti-intellectual. An example of this dumbing down is the current crop of JavaScript "devs." This runs all the way to the semantics of the language: one of the most idiotic decision made in JavaScript is overloading '+' to concatenate strings. Commutativity? Associativity? Algebra? Get out of here with this math nonsense.
So dive on in!
I've only been close to the level but I believe freshman courses of those sorts often involve throwing really smart, cocky kids to a given subject in the most challenging fashion possible. (Remember Caltech used the Apostol text book for calculus, developing everything axiomatically, because, you're smart).
So it is introductory text but it might not a gentle, easy introductory text.
I'm self-taught and it's what made me fully "grasp" programming and the power of abstraction.
One of my favourite pieces of code is a Lisp REPL in Lisp:
(defun repl()
(loop (print (eval (read))))) (-> read eval print loop) (defmacro -> (&rest args)
(loop for item in (reverse args)
for result = (list item) then (list item result)
finally (return result)))The threading macro is great, but the GP one-liner tells me more about lisp.
(->) inserts each form's value between the function name & the first argument in the next form.
(->>) inserts each form's value as the last argument in the next form.
The latter forms are much more niche and I haven't found need for them yet.
It is totally possible to write custom -> macro that works correctly as used in my example.
Though I guess that wouldn't be valid lisp if you tried to express it literally.
while True:
print(eval(input(">>> "))) ovov@ovov ~> cat repl.py
while True:
print(eval(input(">>> ")))
ovov@ovov ~> python3 repl.py
>>> x = 1
Traceback (most recent call last):
File "repl.py", line 2, in <module>
print(eval(input(">>> ")))
File "<string>", line 1
x = 1
^
SyntaxError: invalid syntaxPython's `eval` also isn't sufficient here. For example, you cannot enter that loop you wrote at the prompt it provides when you run it, even if you write it on one line. You could use `exec`, but then everything would print as `None`.
There's the larger point about forms vs strings and printing readably and such, but this doesn't even provide the same basic functionality that a Python programmer would expect from a REPL.
def repl
loop { puts( eval gets ) }
end
I guess I just prefer Ruby for general legibility. The same type of bracket everywhere makes pairing them mentally an error-prone chore. At least for yours truly.puts and gets are string functions. read and print are code deserializers and serializers.
eval is a function that takes a data structure representing code (as deserialized by read) and computes its value as a data structure, and serializes it.
While you might get the same effect as a user, the mechanics and metacircularity are lost.
#define DX 0.0001
typedef double (*func)(double);
double NthDeriv(int n, func f, double x) {
if (n == 1) {
return (f(x + DX) - f(x)) / DX;
}
else {
return (NthDeriv(n - 1, f, x + DX) - NthDeriv(n - 1, f, x)) / DX;
}
}
double Cube(double x) { return x * x * x; }
double result = NthDeriv(3, &Cube, 5.0);
As mentioned in previous discussions of this article, the equivalent in Python is pretty elegant: >>> def deriv(f):
... dx = 0.0001
... def fp(x):
... return (f(x + dx) - f(x)) / dx
... return fp
...
>>> cube = lambda x: x**3
>>> deriv(cube)(2.0)
12.000600010022566But re: the Python code—I would say that, from the perspective of the 1960s, all modern "dynamic" languages that have REPLs (like Python) are Lisps in essential character.
"Lisp", back then, referred less to "a language that uses a lot of parentheses", and more to things like:
• runtime sum-typing using implicit tagged unions;
• parameterization of functions using linked lists (or hash-maps) of paired interned-string "keys" and arbitrary product-typed values, rather than parameterization using bitflags or product-types of optional positional parameters;
• heap allocation and garbage-collection;
• a compiler accessible by the runtime;
• "symbolic linkage" of functions and global variables, such that a named function or variable "slot" can be redefined (even to a new type!) at runtime, and its call-sites will then use the new version.
We only notice the parens as the differentiating feature of Lisps nowadays, because everything else has become widely disseminated. Perl and Python and PHP and Ruby (and even Bash) are fundamentally Lisps, in all of the above ways. Lisp "won."
My point, though, was that while the proponents of Lisp define Lisp one way (by the things only Lisp can do due to e.g. homoiconicity), the opponents of Lisp (like the author was, coming into the course) define Lisp by the set of features that make Lisp "not a Real Programmer†'s programming language"—i.e. the set of things that make them not want to use it.
† http://www.catb.org/jargon/html/R/Real-Programmer.html
My assertion was, from these opponents' perspectives, there are very few languages left for "Real Programmers"; most modern languages have inherited nearly all of those horribly convenient Lisp-isms. Heck—modern CPUs are so good at pointer-chasing, they may as well be Lisp Machines!
On another side of the Smalltalk family tree, languages like Java embraced a kind of static typing and found strong success with it, leading to the promotion of strongly-typed structures over lambdas. This side, ironically, did the most work on JITs, which are meant to compensate for dynamic language features, and languages like Dart are still marrying JIT techniques to strong static type systems today.
And on another side of the Smalltalk family tree, languages like E are fully oriented around objects with rights, so that instead of structures which can be examined, values must have messages sent to them instead. Homoiconicity, lambdas, pattern-matching lists, and other staples of Scheme are nonetheless common in E and Monte.
I think that the Smalltalk family, overall, could be viewed as a response to the barren syntactic landscape of Lisp, but I think that they could also be viewed like any other language: Taking what they like from ancestors, and not taking what they don't like.
That was Lisp back then. Lisp then early on was used in a bunch of symbolic computing domains: theorem provers, computer algebra, natural language processing, planning, rule-based languages, programming language implementation (languages like ML or Scheme were implemented first in Lisp).
In the Lisp sense Python does not have a REPL, since it does not implement READ, EVAl and PRINT over symbolic data, like Lisp does.
You are correct that the analytic derivative isn't that hard to do in any language.
Edit: Should have said "many languages" not just "any"
func cube_3rd_deriv = NthDeriv(3, &Cube); /* returns a function */
double result = cube_3rd_derive(5.0); #include<stdio.h>
#define DX 0.0001
typedef double (*function)(double);
typedef double (*closure_body)(void*, double);
struct deriv_env { function f; };
struct closure { void* env; closure_body body; };
#define CALL(c, x) ((c)->body((c)->env, (x)))
double deriv_body(void *env, double x) {
function f = (deriv_env *)env->f;
return (f(x + DX) - f(x)) / DX;
}
void deriv(function f, struct closure *out) {
out->env = (void *)f;
out->body = deriv_body;
}
double cube(double x) { return x * x * x; }
int main(void) {
struct closure cube_deriv;
deriv(&cube, &cube_deriv);
printf("%f\n", CALL(&cube_deriv, 2.));
printf("%f\n", CALL(&cube_deriv, 3.));
printf("%f\n", CALL(&cube_deriv, 4.));
return 0;
} double Cube3rdDeriv(double x) {
return NthDeriv(3, &Cube, x);
}
&Cube3rdDeriv can now be passed around in a higher-order way. *Main> derivative f x = (f(x+dx)-f(x))/dx where dx = 0.0001
*Main> derivative (^3) 2
12.000600010022566 def deriv(f): return lambda x: (f(x+dx) - f(x))/dx
or even: deriv = lambda f: lambda x: (f(x+dx) - f(x))/dxMaybe that's the misunderstanding in the original article that made it seem so mindblowing?
Evaluating (f(5.0001) - f(5.0)) / 0.0001 just gives an approximation of the derivative at a certain point in the function (aka the "finite difference method").
2017: https://news.ycombinator.com/item?id=14247269 (261 comments)
2013: https://news.ycombinator.com/item?id=5375735 (176 comments)
2009: https://news.ycombinator.com/item?id=504667 (39 comments)
I learned Clojure about a year ago (which was my first introduction to Lisp outside of reading SICP), and it gave me a similar feeling. I felt like Clojure was a "better Java than Java", and now its my go-to JVM language. I think McCarthy was really onto something with Lisp :).
This isn't a particularly unusual approach. Common Lisp, a contemporary Lisp dialect, was designed with efficiency in mind. The approach of "Lisp = slow + inefficient + spending 80% of time on garbage collection" is a myth.
Though we've moved from a period of languages as defined by a spec to languages defined by implementation. Such as Perl, PHP, Ruby and Python. So what is "normal" has changed a lot.
(The ANSI standard was 1994. Still nowhere near 20 years).
#include <stdio.h>
const double delta = 1.0e-6;
double cube(double x) { return x * x * x; }
double deriv(double (*f)(double), double x) { return (f(x+delta) - f(x)) / delta; }
int main()
{
printf("%f", deriv(&cube, 2));
return 0;
}
Am I missing something?but in general a lot of that stuff in the article seems mundane today, you have to imagine seeing it in 1983
How?
If the `deriv` function instead returned a function instead of a number, you could apply the `deriv` function twice.
- you can't write a C macro which will do this symbolically. We really just want to be calculating 3 * x * x.
- having cube and deriv, you can't write an expression which combines these two, and is itself a function.
BTW, the & is unnecessary in &cube; a primary expression which names a function evaluates to a pointer to that function.
constexpr double delta = 1.0e-6;
constexpr auto deriv = [] (auto f) {
return [=] (double x) {
return (f(x+delta) - f(x)) / delta;
};
};
constexpr auto f¨ = [] (auto f) { return deriv(deriv(f)); };
double res = f¨([] (double x) { return x*x*x; })(2.0);I find that it follows the functional spec quite closely, thanks to Go functions being first class citizens.
Here's a much better one that uses a macro to do symbolic differentiation in <150 lines:
https://github.com/aksiazek/symbolic-differentiation
Much harder to do that in C ;-)
Peter Norvig's book "Paradigms of Artificial Intelligence" uses symbolic differentiation as an example in one of the early chapters.
Criticizing it on the basis of parenthesis would be roughly equivalent to criticizing someone talking about the beauty of mathematics on the basis of the color of the piece of chalk they're using. It's true that it's criticism about aesthetics, but it's not criticism about the aesthetics that the mathematician was talking about.
Once you've understood in what sense people mean that Lisp is beautiful, you can disagree, but this disagreement will not concern parentheses.
As an aside: I have a similarly shallow aesthetic aversion to JS syntax for the same reason. When I encounter a 12-line ragged cascade of curly brackets, square brackets and parentheses, sprinkled with semicolons, I wonder how that can be considered OK.
Aside 2: with Lisp, you edit programs structurally, meaning that their position is essentially managed for you. This is what people refer to when they mean that "the brackets disappear after a while." You're not focusing on them; they're handled by something else.
In my editor, I have the closing brackets faded nearly into the background, which reflects how concerned I am about their existence.
It's considered OK because the extra syntax carries semantic weight, and the semicolons disambiguate intent for the interpreter (because while semicolons are optional in javascript, leaving them out can lead to errors.)
And as someone just beginning to play around with (Arc) Lisp, I still can appreciate both paradigms. Neither is objectively wrong.
I'm not sure this is a great presumption. Someone can be familiar with lisps, understand the design choices involved, even deeply appretiate the beauty of the resulting language, while still prefering languages with a more flexible syntax
> This is what people refer to when they mean that "the brackets disappear after a while." You're not focusing on them; they're handled by something else.
This is exactly the problem. Lisps surrender the issue of syntax completely to the interpreter/compiler, which makes it easy for a machine to parse, but harder for a human. I personally prefer syntax to be designed for humans to read and write, because its going to be translated to something different to be executed anyway.
Now, the simplicity of the lisp syntax is of course intimately connected to the homoiconicity of the language and the extremely powerful macros, so I do understand the value of it and the tradeoffs involved. (Although I find it slightly ironic how it's venerated, given that the original lisp actually had two syntaxes, M-expressions for the human to manipulate and S-expressions for the machine to manipulate).
I personally prefer to do my "meta-programming" in the type-system and have flexible syntax to express functions in different ways relevant to the uses of the language. That can be Elm with it's simple type system and elegant operators for application (|> and <|) and composition (>> and <<) which indicate direction, or it can be Agda with it's unicode syntax and custom mixfix operators, where you can define a if-then-else as a function as "if_then_else_ : (b : Bool) -> (x : a) -> (y : a) -> a" and call it "if <b> then <x> else <y>". I find that the way these languages use operators, reduces the need for parenthesis and allows you to express what your code is doing, whether it's a pipeline of functions or some imperative-like steps being done in order. The result is a syntax that I find clearer.
I am however, well aware that a lot of people dislike Haskell's use of operators and that these languages are a minority in terms of usage indicating that my preference might not be common, but I don't necessarily presume that it's because they don't understand Haskell.
There aren't actually more parens in a Lisp program than a program written in a C-like syntax (which is really an algol-like syntax). They just stand out more for two reasons:
1. There is less punctuation in general, so the parens are more obvious. Instead of f(x, y, z) you write (f x y z). Without the commas, the parens stand out because that's all that is left.
2. There is only one kind of parens in Lisp whereas C-like languages use at least three: (), [], and {}, so that makes any particular kind of paren less prominent.
1. Other languages have operators which can be written without brackets, x + y vs (+ x y).
2. Precedence rules allow ex. polynomial expressions to be written without brackets, 2 * x + y vs (+ (* 2 x) y).
3. Algol-like let-bindings or monadic-do-style variable-bindings that inject bindings into the containing block (as opposed to Lisp-style let-blocks where the new scope typically corresponds to a new block) use fewer brackets and keep nesting depth smaller.
# 2 sets of brackets
fun foo(x, y, z) {
let w = x + y;
let u = w + 2*z;
let v = w - 2*z;
u * v
}
# 13 sets of brackets
(fun foo (x y z)
(let ((w (+ x y))
(u (+ w (* 2 z)))
(v (- w (* 2 z))))
(* u v)))Well put, Madam/Sir.
4. The lisp punctuation style closes all levels on a single line, where most C code guidelines close one level per line. This leads to a "thick" chunk of close parens that students of lisp find harder to read at first.
2. The data is also list, and your program is also a list.
3. Algorithm are basically List Manipulation(Stacks, Queues, Trees, Adjacency lists). So its easy to write complex algorithms in Lisp.
4. Tail call recursion. This part amplifies 3. further.
5. Functional programming features.
6. REPL. I mean like a real REPL.
7. Macros.
1 - 7 helps you to express problems and their solutions with code which represents exactly that. The boiler plate and other assisting code is largely non existent.
Another reason is if a thing has been there around for the longest, more people have thought about it. Both quality and quantity of literature of it are higher than others.
Being older doesn’t necessarily mean either quantity or quality of literature will be greater. MUMPS has been around for much longer than Go, for example.
For example, it is a homoiconic language, so any piece of data can be evaluated as if it is code.
The only way to really understand it is probably to learn it :)
Ironically modern Javascript often replicates the bracket pileup, just with "})" instead. Python does away with it by having "nonindented newline" as an invisible semantic character that closes any number of scopes.
I'm still fond of Lisp, but languages like Python, D, Nim (and increasingly, Rust) offer a lot of the Lisp affordances that I really valued -- in particular, easy compile-time metaprogramming. The biggest missing bits are Lisp's deeply integrated REPL-driven programming, and the use of program images -- which have many drawbacks but also some benefits.
IMO, everyone should write and maintain a medium-sized program in a modern Lisp (SBCL, CCL) at least once, just to get an appreciation for how different the programming / debugging experience is. So many well-integrated tools at your disposal, even at runtime.
- Structural simplicity. Instead of having a huge mess of syntax to keep track of, it has only a very small number of constructs. Simplicity is beautiful.
(I don't know lisp) but sometimes this approach just makes the code really difficult to understand afterwards. Does Lisp have something that makes it easier or better?
OTOH distinctions like expressions vs statements don't apply -- my years as a lisp developer were the most productive of my life.
Lisp (or Scheme, in this case) is a very simple language that gets essentially everything right.
As far as syntax goes, the proof is in the pudding. All languages have people gripe about some part of their syntax or another, but it's clear from experience that "OMG parentheses" is exceptionally prevalent. Hand-waving it away as something that people just don't get because of lack of prior exposure is not really sound - somehow other languages don't get similar complaints (at least, not as universally, and not to the same magnitude) with first-time users. Besides, why is there a lack of exposure? Why, because everything else is different. But why is it different? Isn't the obvious conclusion that Algol syntax family is vastly more prevalent for the simple reason that people prefer it, and simple homoiconicity is not sufficiently enticing?
All arguments in favor of Lisp syntax feel like they ultimately boil down to "you're holding it wrong". And that may well be so - but if so many people are finding it so awkward to hold, isn't that prima facie evidence of ergonomic deficiency? I don't claim to understand why Algol-style is easier. Maybe the way our visual processing works is just better with more varied punctuation? That's something for psychologists and brain scientists and maybe linguists to figure out. But in the meantime, we could at least acknowledge the way things are. I really like Lisp as a collection of ideas (not just the usual ones like HOF and macros, but also stuff like e.g. symbol-based namespacing, or the sheer flexibility of CLOS). But lispers have to ask themselves why, instead of Lisp seeing wider adoption, other languages - that came literally decades later, so "upstart" would be a very polite way to describe them in this context! - become vastly more successful than Lisp by appropriating its cherry-picked features.
I have to wonder if you actually know all that much about Scheme. Undelimited continuations, for example, are pretty much strictly broken. I like Scheme, but to claim it's flawless is kinda ridiculous.
Contrast this with most other languages, where there are many things the language does that you can not do. At least, not easily.
At one point I wrote my own Pascal interpreter for the C64. In BASIC, because I wasn't good enough to do it in assembly. It was a little slow.
I desperately wish someone had introduced me to lisp. The entire course of my life would have been different. The most frustrating thing is that I probably could have written a naive lisp interpreter in assembly.
However, I very well remember reading the Compilerbau book by N. Wirth and the awe I felt. It’s just a few pages but it was a revelation.
They are all available on the ETH website and his homepage (in various revisions). Basically everything you need to know about imperative programming on CS bachelor level for free.
I now focus primarily on Rust, which has given me a lot of the same feelings as when I first encountered Lisp. It's truly a language ahead of its time, and I hope it will continue to grow beyond what even Lisp was able to accomplish.
https://gist.github.com/divs1210/4ca74577711eb996a89a36d86a3...
Well, not that simple... as the author hasn't taken the potential problem of free variable capture into account.
They seem available in other functional languages.
I was under the impression LISP was unique due to its macros.
Joking aside, I think lisp is nearly fundamental in the same way that c is.
i mean, if i have a lisp with static types vs not static types, why is the lisp part important at all? and you just have two different languages one with static types and one without?
seems very unimportant? i dunno could someone enlighten me
Also, up to that point the author has only programmed in a couple of different assembly languages, plus Basic. Coming at it from that perspective, there's plenty of novelty to be found in Lisp family languages.
The author seems to be making the case that, contrary to his original skepticism, Lisp is indeed some magical higher-order language, but I honestly don't see it. Lisp's ratio of philosophizing to noteworthy projects seems to tend toward infinity.
Like others writing here and like the author of the post I, too, had my imagination fired by SICP and the culture it fits in. When I pick up a Learn to Program book and the examples are managing a used car dealer's inventory, I can only say "Thanks" to Abelson and Sussman. They turned me on to something exciting.
(deriv (deriv f))
or if you wanted to take the nth derivative
(define (nth-derivative f n)
(if (= n 0)
f
(deriv (nth-derivative f (- n 1))))
You could do it with some data structures, which you'd then have to interpret, but not as nicely or easily. #define DX 0.0001
typedef double (*func)(double);
double NthDeriv(int n, func f, double x) {
if (n == 1) {
return (f(x + DX) - f(x)) / DX;
}
else {
return (NthDeriv(n - 1, f, x + DX) - NthDeriv(n - 1, f, x)) / DX;
}
}
double Cube(double x) { return x * x * x; }
double result = NthDeriv(3, &Cube, 5.0);but in 1983 this would have seemed magic.;