Last active
December 23, 2015 21:19
-
-
Save jeremyheiler/6695635 to your computer and use it in GitHub Desktop.
variable and function definitions for a javascript-based lisp?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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)) |
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.
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
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 withlambda
. The difference between a var and a function is the first argument afterdefine
. If it's a symbol, then it's a var, and if it's a list, it's a function.