Looking at the coding style he certainly wrote every single bit by his own.
The code itself is standard of the shelf lisp introduction as done by good universities in the last decades. (SICP, Lisp in Small Pieces, ...). But bringing this simple code to the masses who just known C, python, perl, java, php or ruby is the interesting part. They can now relate to eval, tailcalls or an env.
The forth and OCaml implementations were written from scratch by chouser (Chris Houser of "Joy of Clojure"). The rest were written from scratch by yours truly (kanaka/Joel Martin). However, it looks like there are a number of other implementations happening by other people now that HN has made it more visible.
Very cool, I am going to have to try out the C one. The only thing missing is HDL (Verilog or VHDL) but there are tons of those out there (e.g. https://github.com/jbush001/LispMicrocontroller)
The fact that C# came out with 36% more lines than Java would make me doubt much extrapolation about productivity from these numbers. You can do a token for token substitution of Java and end up with close to compilable C# code; there are only a handful of things Java has that C# doesn't, while C# has many more abstraction tools.
I'm willing to bet that most of the Java code could be made shorter with the use of more functional style code in Java 1.8 (which might be why he labeled it "Java 1.7")
Note that `return None` isn't needed - Ruby doesn't have it, so that's a "wasted" line. The function should also be more like
def prn(*args):
print(" ".join(printer._pr_str(exp, True) for exp in args))
or, if Python 2 support wasn't needed,
def prn(*args):
print(*(printer._pr_str(exp, True) for exp in args))
Defining these out-of-line is a good idea, though (it improves introspection in Python).
`mal_readline` has a bit of support for Python 2 which takes a few lines, but mostly it's code like
try:
with open(histfile, "r") as hf:
for line in hf.readlines():
pyreadline.add_history(line.rstrip("\r\n"))
pass
except IOError:
print("Could not open %s" % histfile)
pass
The Python code handles errors but has two pointless `pass` statements and a couple of odd choices. It should better be:
try:
with open(histfile) as hf:
for line in hf:
pyreadline.add_history(line.rstrip("\n"))
except IOError:
print("Could not open %s" % histfile)
Note that the Python uses lazy reading which is why it needs explicit closing; Ruby likely would too if it read lazily.
I have no idea what's up with `mal_types`/`types`; Ruby does simple stuff and then Python does... something. I will say that the Python can be a lot simpler at minimum, although I'd need to grok what it does before I can say what. For example,
elif _hash_map_Q(a):
akeys = a.keys()
akeys.sort()
bkeys = b.keys()
bkeys.sort()
if len(akeys) != len(bkeys): return False
for i in range(len(akeys)):
if akeys[i] != bkeys[i]: return False
if not equal_Q(a[akeys[i]], b[bkeys[i]]): return False
return True
can be replaced with
elif type(a) is Hash_Map:
return a.keys() == b.keys() and all(equal_Q(val, b[key]) for key, val in a.items())
I think the `_Q` suffix is a the writer missing Ruby's `?`-suffixes on identifiers.
There's also a lot of mess that I think arises because the writer might not be aware that you can make callable objects in Python.
I think in the end it comes down to language experience. My Ruby would probably look as hacky as Joel Martin's Python. I doubt there would be as large a difference if they were both written with the same language familiarity.
Python is one of the oldest implementations and I ought to do a another pass through to clean it up sometime.
I'm happy to take pull requests that make an implementation shorter and/or more idiomatic as long as it doesn't change the overall order/flow (and doesn't break any of the tests of course).
oh sorry, i meant if we can have a tiny C, or C-like compiler implementation. :)
(although having a C implementation is awesome, even with the caveat)
lisps are great as an instruction on the subject because of the clean and elegant design, but a C compiler requires tackling all kinds of very nasty problems...
i'm sure it would be very valuable for people wanting to go even further with gaining a practical understanding of programming language/library implementation
I like how it takes an incremental, step-by-step approach. One of my favourite papers is Ghuloum's "An Incremental Approach to Compiler Construction" [1], which shows how to build not a mere interpreter, but a _compiler_ to actual machine code, in twenty-four steps, starting from simple integers and proceeding through more and more advanced topics.
Shameless plug: I've been trying to follow along, but making it entirely self-contained and avoiding any additional tools. The result (in very early stages, currently dormant but not quite dead yet) is called Lithium [2].
I'm working on a lisp interpreter right now, so this should be useful for reference.
One question, how would I go about implementing tail call recursion? I's assume that's a pretty important thing to have in lisp (or any functional language for that matter). I'm working in ruby, if that matters.
> Modern polyglot developers do not memorize dozens of programming languages. Instead, they learn the peculiar terminology used with each language and then use this to search for their answers.
This is the clearest explanation of how polyglot programming works I ever read.
Great work in general, very valuable resource for studying and comparing different languages. I'd like to add one more resource to the mentioned in the guide: http://rigaux.org/language-study/syntax-across-languages/ - it's similar to hyperpolyglot, but has comparisons between non-related languages too. And focuses solely on syntax.
43 comments
[ 4.5 ms ] story [ 103 ms ] threadThe code itself is standard of the shelf lisp introduction as done by good universities in the last decades. (SICP, Lisp in Small Pieces, ...). But bringing this simple code to the masses who just known C, python, perl, java, php or ruby is the interesting part. They can now relate to eval, tailcalls or an env.
C# - 7.9% C - 7.2% Rust - 7.1% Visual - Basic - 6.6% Makefile - 6.5% Java - 5.8% Forth - 5.0% Go - 4.5% PostScript - 4.2% Haskell - 4.0% Shell - 3.8% Perl - 3.5% JavaScript - 3.4% Matlab - 3.2% Scala - 3.1% PHP - 3.1% OCaml - 3.0% Lua - 2.8% R - 2.7% Python - 2.7% CoffeeScript - 1.9% Ruby - 1.9% Clojure - 1.9% Scheme - 1.7% CSS - 1.4% Other - 1.1%
It seems you could extrapolate interesting theories regarding productivity using these numbers.
There's a jump between Ruby/CoffeeScript and Python, and I wonder why.
https://github.com/kanaka/mal/blob/master/cs/core.cs#L272
Could easily be turned into one line ... not that any sane person would want to, though.
`core` has inline lambdas in Ruby:
but separately defined ones in Python Note that `return None` isn't needed - Ruby doesn't have it, so that's a "wasted" line. The function should also be more like or, if Python 2 support wasn't needed, Defining these out-of-line is a good idea, though (it improves introspection in Python).`mal_readline` has a bit of support for Python 2 which takes a few lines, but mostly it's code like
versus The Python code handles errors but has two pointless `pass` statements and a couple of odd choices. It should better be: Note that the Python uses lazy reading which is why it needs explicit closing; Ruby likely would too if it read lazily.I have no idea what's up with `mal_types`/`types`; Ruby does simple stuff and then Python does... something. I will say that the Python can be a lot simpler at minimum, although I'd need to grok what it does before I can say what. For example,
can be replaced with I think the `_Q` suffix is a the writer missing Ruby's `?`-suffixes on identifiers.There's also a lot of mess that I think arises because the writer might not be aware that you can make callable objects in Python.
I think in the end it comes down to language experience. My Ruby would probably look as hacky as Joel Martin's Python. I doubt there would be as large a difference if they were both written with the same language familiarity.
I'm happy to take pull requests that make an implementation shorter and/or more idiomatic as long as it doesn't change the overall order/flow (and doesn't break any of the tests of course).
Then I saw GNU Make.
Caveat, no GC. Although that could probably be added easily using the Boehm GC. I plan to do so at some point assuming nobody beats me to it.
(although having a C implementation is awesome, even with the caveat)
lisps are great as an instruction on the subject because of the clean and elegant design, but a C compiler requires tackling all kinds of very nasty problems...
i'm sure it would be very valuable for people wanting to go even further with gaining a practical understanding of programming language/library implementation
Shameless plug: I've been trying to follow along, but making it entirely self-contained and avoiding any additional tools. The result (in very early stages, currently dormant but not quite dead yet) is called Lithium [2].
[1]: http://scheme2006.cs.uchicago.edu/11-ghuloum.pdf
[2]: https://github.com/nathell/lithium
One question, how would I go about implementing tail call recursion? I's assume that's a pretty important thing to have in lisp (or any functional language for that matter). I'm working in ruby, if that matters.
https://github.com/kanaka/mal/blob/90ab31710e9a495ba3aebf794...
The Maven, HTML, C headers, and CSS are just support.
Personally I found Forth implementation the most impressive, even more so than bash. Well, Make comes close :)
- forth
- mal itself (e.g. self-hosting: all implementations can run this)
- miniMAL (Lisp interpreter in < 1024 bytes of JS)
- PostScript
- R
- Racket
This is the clearest explanation of how polyglot programming works I ever read.
Great work in general, very valuable resource for studying and comparing different languages. I'd like to add one more resource to the mentioned in the guide: http://rigaux.org/language-study/syntax-across-languages/ - it's similar to hyperpolyglot, but has comparisons between non-related languages too. And focuses solely on syntax.