by peregrine · 12y ago · 0 comments
elixir : erlang vm (BEAM) :: clojure : jvm
Elixir's syntax is heavily influenced by Ruby but its semantics and structure is very reminiscent of Clojure. Like Clojure you are constantly reminded you live on JVM, with Elixir you are constantly reminded that Erlang is under the covers and thats OKAY.
Its really great stuff and the creator José Valim has really put thought, love and care into every part of the project. I highly suggest you take a look, you might be surprised at what you find.
That's what many people see as the reason for homoiconicity after all. I put it in quotes, but maybe I should have said something about how languages can support sophisticated Lisp-like meta-programming without being a Lisp at all.
Nonetheless, as I'm still learning (aren't we all?), I would love to hear what a truly homoiconic language has above something like Elixir with its quote, unquote and unquote_splicing. By truly homoiconic I mean that AST is equivalent to the written form.
EDIT: I now realise you're not only rvirding, but that you wrote Lisp Flavoured Erlang https://github.com/rvirding/lfe so therefore (I hope) are in an even better position to educate!
In Lisp you are free to create completely new syntactic forms. Whether this is a feature of the homoiconicity of Lisp or of Lisp itself is another question as the Lisp syntax is very simple and everything basically has the same structure anyway. Some people say Lisp has no syntax.
Trying to build the AST that directly in elixir is quite difficult as the AST is not trivial even if it relatively simple and regular. Quote and unquote in macros do a lot of the work for you. At one level you could do something similar for Erlang but no one has actually gone all the way with it. Erlang uses parse transforms for that. They are more powerful but much more complex to use.
It has immutable data but not immutable bindings. Erlang has both as immutable. What that means is, you can do x = value1 then x = value2. In Erlang, like in mathematical notation, once you said X=value1 it is value1, it doesn't make sense to say X=value2 (it throws an exception). In either case you can't modify value1 in place for example by saying x[3]=4 or X[3]=4 ( <- I am making up syntax to illustrate here).
Some like it one way some like it another.
Elixir has better meta-programming, which is nice.
In general, Erlang syntax is not the steepest learning curve. The steepest learning curve is to learn to use actors for concurrency and use functional programming for sequential parts. Elixir presents a more familiar syntax but the other learning curve parts are still there.
I can implement any data structure, and if I define reduce for that data structure I get the entire Enum module for free, all tail call optimized.
It means I can mix and match data structures. I can do crazy things like zip a binary search tree and a lazy data stream together and then call take(5) to get the first 5 elements. And all that is required is reduce to be defined for all data structures. Protocols are a big deal.
Especially when a good chunk of developers come to expect to have basics like strings and library/syntax to support them natively.
Erlang isn't horrible, and when you learn Elixir you WILL come to learn Erlang and its documentation.
So X = [1,2,3]. in gives you variable binding X and variable value [1,2,3]. Variable value [1,2,3] is immutable in both Elixir and Erlang. You cannot do x[0]=5 in either one and expect the value [1,2,3] to be modified in place. Binding X is immutable in Erlang but not in Elixir. In Erlang you'd do X1=[5,2,3]. and in Elixir you could do x=[5,2,3].
You can argue what is better. I think both approaches are good, there is not one clearly superior in my opinion. Sometimes I take your side and prefer Erlang, because I like having explicit and immutable variable bindings that don't change behind my back. If I make X=[1,2,3], it is going to be [1,2,3] from now until forever.
As far as Actor and distribution stuff all that goodness is still in Elixir. They complement each other more than compete. Both take advantage of the awesome VM (BEAM) -- which I think is a marvel of engineering -- concurrent garbage collection, lightweight processes, scheduling balancing, async IO background threads, binary references, distribution and so on.
a = [1,2,3,4]
x = a
x = [1,2,3,4,5]
a == [1,2,3,4] => true
x == [1,2,3,4,5] => true
This is empirically NOT the same as ruby or even mutability.
This is a HUGE misunderstanding of Elixir. Once something is created you cannot modify it, PERIOD, you can rebind variables within the same scope. You cannot change the values.
Same thing is possible in clojure, and nobody questions that its immutable.
EDIT: And to be extra clear this is only within the same function. Not possible across functions, modules or processes.
All of this while basically living in erlang. You can call erlang functions with a slightly different syntax, :erlang_module.function_name, and you can natively use erlang packages and files with mix(it checks for rebar/makefiles and makes educated guesses on how to build them, or you can configure it).
If you think its a benefit of Erlang its probably also a benefit to Elixir.
EDIT: I also forgot you get things like the threading macro(|> from clojure et al), built in testing framework, and a VERY active and helpful community on irc #elixir-lang and the mailing list.