197 points · by munificent · 13y ago · 108 comments
I think the origins of the "JavaScript is Scheme!" meme have more to do with the fact that you can pretty trivially re-implement all of the code from the Little Schemer (including the applicative order y-combinator) in JavaScript, than any of the reasons mentioned here. JavaScript still, imho, has nicer closures and lambdas than most mainstream programming languages.
Actually, thinking back on it, I remember having a conversation with Prof. Daniel Friedman about how similar the two language are and I believe his response was something along the lines of: if you have the lambda calculus, what else do you need?
Final nitpick: most of the "Y isn't really a lisp!" (another meme) articles I've read always miss what I think is the biggest thing missing from all languages compared to any lisp: symbolic expressions. To me this is the single most underrated feature of lisp that almost no other programming languages offer.
So I think it's worth remembering that it's really a very recent development that we can now treat it as obvious that a language like JavaScript would contain the things it has in common with Scheme.
When I first started thinking about writing this post, I was considering going from the angle of "JS was Scheme when Crockford said that but now every language has caught up and we're all Scheme now" but somehow a different post fell out of my head when I started writing.
And let's not forget great book Higher-Order Perl: Transforming Programs with Programs http://hop.perl.plover.com/
That's spurious. Anything that can be implemented in one Turing complete language can be implemented in any Turing complete language. The question is how easy it is.
I would never claim that Javascript is scheme (and it doesn’t sound like that’s what you’re claiming), but the things I love about Javscript are also things I love about scheme.
Is it the homoiconic-ness of the s-expressions you like?
Personally, I'd love to see continuations in Javascript, despite the potential for horrible abuse. Deeply nested callbacks are a pain in the ass. Of course, promises are a pretty decent solution.
I was recruited to Netscape with the promise of “doing Scheme” in the browser...
I’m happy that I chose Scheme-ish first-class functions and Self-ish (albeit singular) prototypes as the main ingredients.
https://brendaneich.com/2008/04/popularity/
So "JS=Scheme" is not very crazy. Maybe it oversimplifies. But it's not baseless. And it's short.
Obviously JavaScript isn't Scheme using the author's "top 10 defining characteristics of Scheme." Crockford's analysis is on track - and John Resig and Bear Bibeault highlight the same idea by introducing functions are really fundamental to the languages more than objects (in "Secrets of a JavaScript Ninja").
The language's history does imply the comparison as well. Eich's original mandate was to "write Scheme for the browser" but he was later directed to give his language a C-like syntax and to ride on the coat-tails of the hype surrounding Java.
"JavaScript isn't Scheme" might not be accurate for folks schooled in the finer points of Scheme. It is very useful to those who cut their teeth learning Java.
Anyone trying to draw parallels between Javascript and Scheme isn't crossing some pedantic line, they're off in crazyland. Or, more likely, they don't understand both languages as well as they're implying.
it breaks the connection between JavaScript and Java.
This is a good point. The defining characteristic of Scheme in the minds of many is really just closures. Almost every modern language except Java has those, so "Scheme" has inadvertently turned into a weird shorthand for "inverse of Java".I think that's too blunt of an instrument to help you reason about any of the languages in question, but I do appreciate encouraging people to think functionally.
That's not true. In fact, there are some prominent people in the Lisp community who do not think that Scheme is a Lisp.
See this thread from 2002 on comp.lang.lisp (especially Pitman and Naggum's comments) for more. If you are too young to have posted on C.L.L when Usenet was still popular, and want to see what a full fledged flame war looks like, look at the end of the thread.
Some people who posted there (including the OP) are on HN.
https://groups.google.com/forum/#!topic/comp.lang.lisp/Bj8Hx...
OTOH Lisps like Emacs Lisp, Common Lisp, ISLisp, and some others are still carrying the core of the first Lisp. Basically you can run Lisp 1.5 code in those without many changes.
Just like Java is not the new C. OTOH languages like C++ and Objective C still support much of C. You can port C to C++ easily.
Over the history many other languages have been trying to replace Lisp: ML (functional programming), Dylan (object-oriented programming), Logo, Scheme, ... all tried to modernize/improve Lisp. Now it is Clojure and some other languages.
Still there are core Lisp languages which carry on the genes.
But not Clojure:
Clojure 1.5.0-RC2
user=> (car '(1 2))
CompilerException java.lang.RuntimeException: Unable to resolve symbol: car in \
this context, compiling:(NO_SOURCE_PATH:3:1)
What? user=> (append '(1 2) '(3 4))
CompilerException java.lang.RuntimeException: Unable to resolve symbol: append \
in this context, compiling:(NO_SOURCE_PATH:4:1)
What? user=> (assoc 'foo '((foo . 3)))
ArityException Wrong number of args (2) passed to: core$assoc clojure.lang.AFn\
.throwArity (AFn.java:437)
user=> (atom 'foo)
#<Atom@2285af04: foo>
???Does not look like it supports Lisp processing as defined by McCarthy:
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.91....
Additionally the Java error messages are especially irritating. I enter Clojure code and get Java error messages.
Compare that with a real Lisp:
bash-3.2$ java -jar abcl.jar
Armed Bear Common Lisp 1.1.1
Java 1.7.0_25 Oracle Corporation
Java HotSpot(TM) 64-Bit Server VM
Low-level initialization completed in 0.628 seconds.
Startup completed in 1.968 seconds.
Type ":help" for a list of available commands.
CL-USER(1): (car '(1 2))
1
CL-USER(2): (append '(1 2) '(3 4))
(1 2 3 4)
CL-USER(3): (assoc 'foo '((foo . 3)))
(FOO . 3)
CL-USER(4): (bar 10)
#<THREAD "interpreter" {6EEE1E2}>: Debugger invoked on condition of type UNDEFINED-FUNCTI\
ON
The function BAR is undefined.
Restarts:
0: CONTINUE Try again.
1: USE-VALUE Specify a function to call instead.
2: RETURN-VALUE Return one or more values from the call to BAR.
3: TOP-LEVEL Return to top level.
[1] CL-USER(5):
We even get Lisp error messages. The compiler is written in Lisp.
http://svn.common-lisp.net/armedbear/trunk/abcl/src/org/arme...Clojure may be a cool language, but the core of Lisp has been replaced in parts with something else. List processing is different. It's now based on 'persistent' lazy sequences...
In Clojure, your examples are spelled:
(first '(1 2))
(concat '(1 2) '(3 4))
(get {:foo 3} :foo) ; or (get {'foo 3} 'foo)
(symbol? 'foo) ; or (not (coll? 'foo))
And lots of things in the world irritate me for reasons that have nothing to do with whether they are Lisp or not.I'd like to point out that your examples wouldn't have worked in Lisp 1.5 either. You would have had to type, for example,
CAR (QUOTE (1 2))
interactively.How are they not. Okay, I see your argument with Clojure, but how does that apply to Racket?
Welcome to Racket v5.3.4.
> (car '(1 2))
1
> (append '(1 2) '(3 4))
'(1 2 3 4)
> (assoc 'foo '((foo . 3)))
'(foo . 3)
>Epic.
I started posting on Usenet ~1989. I've seen a number of flame fests that I considered epic but this moved the bar exponentially.
I hesitate to ask this question because I really don't want to start a flame war.
[1] - Immediately Invoked Function Expressions: (function(){ ... }())
In scheme, lexical scoping is easily accomplished through the let form, and it's pretty hard to make a variable global by mistake.
So yes it does have lexical scoping in the sense that function arguments are lexically scoped, but that's the only scoping environment, and you can't hide that fact behind macros like you could in scheme if it was needed.
There is; it's just ugly. You just have to use an immediate function as the body of the if statement.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
It seems like this argument conflates lexical scoping with the requirement that variables be lexically scoped. Lexical scoping (even if optional) is extremely useful for closures. Requiring lexical scoping would very useful for safety.
function foo() {
console.log(whereDidThisComeFrom);
}
window.whereDidThisComeFrom = "the damn global object";
foo();
And: function foo() {
var a = {
whereDidThisComeFrom: "a, of all places"
};
with (a) {
console.log(whereDidThisComeFrom);
}
}
foo();
In both cases, you can't lexically resolve the identifier whereDidThisComeFrom.(To be strict, that seems to be more a matter of what a scope is, rather than if the scoping is lexical or dynamic.)
Strict mode in ES 5 does address the first two, and "let" in ES 6 addresses the latter, so that's good. But at that point, you're talking about the future and not JavaScript as it is today.
This is not lexical scoping:
function example(k) {
function ugly() {return i};
for (var i = 0; i < k; i++) {}
return ugly;
}
example(4)() // => 4The i in ugly is lexically resolved to refer to a particular declaration, the one on the line below it. Scheme's lexical scoping rules are not the only possible lexical scoping rules. A lexical scoping system does not cease to be lexical simply because it is not the same as Scheme's lexical scoping. For it to be non-lexical, the scoping would have to depend on something other than the lexical structure of the program. For example, you could just as well have a dynamically-scoped JS in which ugly's return value would depend on its caller's binding for i, and in which calling a function with a free i from within example would make example's binding for i visible to that other function, but that isn't the way JS works.
It's quite reasonable to argue about whether that scoping is sane, and indeed with "let", the ES folks are giving us a saner binding construct; but it is certainly the case that the relationship between the declaration and the use of a variable is established by the lexical structure of the program text, not the dynamic structure of the executing program.
I think that what you are arguing is that Javascript is statically scoped.
http://www.adequatelygood.com/JavaScript-Scoping-and-Hoistin...
Hoisting generally seems ugly to me.
Edit: I think there is some confusion here about scoping. One axis is dynamic versus lexical scoping (are there other options?). Another axis is function, block, global, etc. scope. They seem orthogonal to me.
Have JS developers taken a bunch of compiler theory terms without understanding them and applied them to Javascript? I have to say, I'm not really surprised...
The level of scope (global, block, ...) interact with the type of scoping in many ways, so I would not say that they are orthogonal. This example would behave very differently under dynamic and lexical scoping.
function foo() {
var i = 1;
function bar() {
var a = i;
var i = 2;
return a + i;
}
return bar;
}
foo()() // => ? function foo() {
window.c = "really?"
d = {};
function bar() {
var a = "var";
{
with (d) {
var a = b;
}
}
console.log(a);
}
d.b = c;
bar();
}
foo();Chapter 1: The Early Years: http://www.youtube.com/watch?v=JxAXlJEmNMg
Chapter 2: And Then There Was JavaScript: http://www.youtube.com/watch?v=RO1Wnu-xKoY
JS has inherited from both Scheme and Self.
I think the thing that makes the comparison is more the first class functions than the closures. The closures assist the first class functions, but the functions are the star.
Not a whole lot of non functional programming languages support first class functions with the simplicity and completeness of javascript.
http://en.wikipedia.org/wiki/First_class_function#Language_s...
In javascript, passing functions as parameters is a day to day thing, and that style of being able to pass around different functions as building blocks is what gets it compared to functional languages. Sure, that's not the only thing that functional languages have going for them, but it's the most important thing, and what they're even named after.
But people like to repeat slogans without understanding. This is how everyone is calling Clojure a Lisp, for example, while it is a language of its own, which happen to use parenthesized syntax.)
It is not parenthesis that makes a language Lisp. It is not first class functions that makes it Scheme.) Lisp/Scheme is a set of conventions/features, and when some are broken and other are missing that means it is something else.
This is in contrast to JavaScript, whose creators never claimed it was Scheme or even a dialect, although the author of the article suggests it was influenced Scheme. In addition, he has perceived that lately, many people claim JavaScript is Scheme, and he rejects that claim.
Disputing the validity of Clojure as a Lisp is orthogonal to the subject of the article.
It is possible, for example, to go through some books, especially "The Joy Of Clojure" which contains 20 line of marketing slogans for 1 line of code, and make explicit commentaries on all the subtle differences, but I'm not going to perform such a tedious task for free.)
> "The Joy Of Clojure" which contains 20 line of marketing slogans for 1 line of code
> but I'm not going to perform such a tedious task for free
Well, obviously there's no point in discussing this further and we have to agree to disagree. Have a nice day anyway.
This definition seems to exclude Common Lisp from being a Lisp. That might be a defensible position, but it does call into question how your definition of "Lisp" is useful to you. It also seems to exclude languages like Dylan, which are commonly regarded as Lisps by people far cleverer than me.
Well, the official website doesn't help much by itself calling Clojure a (dialect of) Lisp.
The only other time I heard someone saying "Clojure is not Lisp" it wasn't in order to say something nice about the former.