4 ms·
Well, I wrote a genetic programming library, and it was fun to parse a Lisp-like representation from Python. You still have recursion and everything (albeit no
by danuker 4y ago
Well, I wrote a genetic programming library, and it was fun to parse a Lisp-like representation from Python. You still have recursion and everything (albeit no tail call optimization).
Here, `_from_source` goes from a plain array of tokens to a nested one (tree), depending on their arity:
https://github.com/danuker/symreg/blob/7c6593d3046f6c52dfb92f5c697ff7f10d036165/symreg/ga.py#L156 https://github.com/danuker/symreg/blob/7c6593d3046f6c52dfb92...
S-Exps are almost valid Python. The exception is the single-element tuple which needs a comma: (x,)
But I still preferred to use Python as a programming language, and Lisp as a sort of AST. It's just easier. I am curious what roadblocks you faced in your ASCII delimited parsing.
Do you by any chance still have the two parsers? I'd love to see them. If you are worried about your anonymity, you can find my e-mail on my blog, and my website on my HN profile. I promise not to disclose your identity publicly.
- throw009 4y ago>Here, `_from_source` goes from a plain array of tokens to a nested one (tree), depending on their arity: You're 90% there. Lisp notation obviates the need for arity tracking, which is why in lisp + and sum are the same function: scheme@(guile-user)> (+) $416 = 0 scheme@(guile-user)> (+ 1) $417 = 1 scheme@(guile-user)> (+ 1 2) $418 = 3 scheme@(guile-user)> (+ 1 2 3) $419 = 6 Add higher order functions, e.g. (λ (x) (x x)), and lisp notation is the simplest/only way to deal with the general case where you don't know ahead of time the arity of the function you'd be applying because of partial currying and data persistence. As for the parsers this was 10 years ago at university. I've long since lost the source code. There weren't any problems with python, it's just that once I wrote the lisp version I realized just how useful the brackets actually are. There's a reason why every computer language is more or less context free. Lisp just takes that to its logical conclusion.
- danuker 4y agoWell, thank you for your feedback. I don't know why I bothered with the flat list in the first place. I might rewrite this library in Clojure. And I might blog about my findings. Cheers!
- noloblo 4y agoThanks between clojure sbcl racket and scheme which is the fastest language of am lisps?
- butt___hugger 4y agoSBCL, probably. Scheme would be a close second depending on the implementation (Chicken, Gerbil or other compile-to-C impls.) Racket and Clojure are closer in performance to scripting languages like Python.
- danuker 4y agoIn the TechEmpower benchmarks, Clojure is about 2.75x slower than Java, while Python is 5.59x slower. The fastest composite Python "framework" is uvicorn at 17.9% of the fastest run in Java (officefloor). The fastest Clojure run is Aleph at 36.4%. https://www.techempower.com/benchmarks/#section=data-r21&l=zijzaz-6az&test=composite https://www.techempower.com/benchmarks/#section=data-r21&l=z... About SBCL, I get conflicting results. The Benchmarks Game shows it roughly as fast as Java for many problems: https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/lisp.html https://benchmarksgame-team.pages.debian.net/benchmarksgame/... But here it's significantly worse than the Clojure runs, likely due to the slow DB bindings (the other test types show it roughly in line with Clojure; though it uses a "Stripped"/unrealistic HTTP implementation): https://www.techempower.com/benchmarks/#section=data-r21&l=zik0zf-6bf&a=2&test=fortune https://www.techempower.com/benchmarks/#section=data-r21&l=z... I am not sure which Lisp implementation is used; is it SBCL? Does this file speak to you? https://github.com/TechEmpower/FrameworkBenchmarks/blob/73eb2abec4e5c594e40812250ebe061234fe9473/frameworks/Lisp/woo/woo.dockerfile https://github.com/TechEmpower/FrameworkBenchmarks/blob/73eb...
- lycopodiopsida 4y agoThere is a fun blog series about comparison and tweaks of the same problem between java, rust and CL. I will link only the last part of the series[1], but the general take is that SBCL performs comparably to a heavily optimized java implementation, and can even hold a candle to rust and java on short runs. [1] https://renato.athaydes.com/posts/revenge_of_lisp-part-2.html https://renato.athaydes.com/posts/revenge_of_lisp-part-2.htm...