Aslan French
Aslan French

Reputation: 559

Are javascript function expressions analogous to or based off s-expressions?

I've read that javascript was in part based on Scheme, a dialect of LISP. When I was reading about this and LISP, it struck me that the javascript function expression syntax seemed like a similar kind of structure. My understanding of s-expressions is that all syntax is either treated as an atom, or s-expressions which can then be recursively referenced, and is eventually reduced down to an atom. If a javascript function treats the creation and reference of a function as just another variable (and arrays, strings, and numbers can all be assigned to variables too) does that mean that the javascript function expression is essentially a implementation of the s-expression idea?

Upvotes: 2

Views: 456

Answers (3)

Rainer Joswig
Rainer Joswig

Reputation: 139261

S-expressions (short for symbolic expressions) are a data format (like for example XML, JSON, ...). S-expressions are built out of lists, symbols, numbers, strings and other data objects.

Lisp data and code is typically written as s-expressions in an external textual format or created by functions.

Creating a nested list:

CL-USER 1 > (list '* 10 (list '- 3 5))
(* 10 (- 3 5))

The above result is actually a nested list of symbols and numbers, not a string.

Actually executing this list as a program:

CL-USER 2 > (eval (list '* 10 (list '- 3 5)))
-20

JavaScript does not have the direct equivalent of this:

  • it does not write code in such data structures. JavaScript programs are text

  • JavaScript can evaluate source code with its eval, but the source code is a string

JavaScript function objects are unrelated, since they are not source code themselves, have no textual format, etc.. Taking arguments and computing with them also makes them not source code and don't make them dealing with the equivalent of S-expressions. S-expressions are used in Lisp to represent source code just like other data.

Upvotes: 3

MinusFour
MinusFour

Reputation: 14423

I would say no. They are analogous to lambda expressions in lisp. S-expressions are more about the general structure of your code and data. You can kind of write S-expressions with functions in javascript or at the very least mimic the nested structure but I wouldn't go as far as saying they are analogous.

Upvotes: 1

river
river

Reputation: 1028

Sort of, they are first class functions which you can pass around as values.

This is very close to scheme's lexically scope lambda and lisp is the first language family to have this feature. but instead of the javascript function parameters having lexical they have "function scope".

Javascript's "function scope" is a bit less mathematically pure than lexical scope but it's a minor difference.

Regarding s-expressions, javascript has no equivalent to this - you can't "quote" pieces of javascript code to get a js AST object.

Upvotes: 1

Related Questions