Skip to content

Instantly share code, notes, and snippets.

@jeremyheiler
Last active December 23, 2015 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyheiler/6695635 to your computer and use it in GitHub Desktop.
Save jeremyheiler/6695635 to your computer and use it in GitHub Desktop.
variable and function definitions for a javascript-based lisp?
;; javascripty
(var foo 42)
(var inc (function (x) (+ x 1)))
(function dec (x) (- x 1))
;; clojurey
(def foo 42)
(def inc (fn [x] (x + 1)))
(defn dec [x] (- x 1))
;; schemey
(define foo 42)
(define inc (lambda (x) (+ x 1)))
(define (dec x) (- x 1))
@jeremyheiler
Copy link
Author

The upside for the javascripty version is that it's familiar to JavaScript developers. A potential downside is that function is used for both a function declaration and an anonymous function. I'm not sure if that is a technical problem, though. At the very least, the opportunity to disambiguate would be lost.

The clojurey one is very succinct. It clearly disambiguates between a function declaration and an anonymous function. The downside is that this would look exactly like ClojureScript, and that might not be a good idea.

The schemey way is tried and true. While it reuses define for both scenarios, it differentiates anonymous functions with lambda. The difference between a var and a function is the first argument after define. If it's a symbol, then it's a var, and if it's a list, it's a function.

@DarrenN
Copy link

DarrenN commented Sep 25, 2013

I prefer consistency so would prefer either def or define over var. I don't want to think too hard about JS when writing in another language.

@jeremyheiler
Copy link
Author

I think the only ways the javascripty solution could work is if it always create a function by assigning it to a var, or if I am able to determine if the function being created is in the statement or expression position. I don't want to do the former because the goal is to be bi-directional. "Familiarity" asside, I like the schemey approach here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment