9 ms·
I've been hearing claims my entire programming career about how Lisp is supposedly "superior" to mainstream programming languages, but I've never seen a concise
by p-e-w 4y ago
I've been hearing claims my entire programming career about how Lisp is supposedly "superior" to mainstream programming languages, but I've never seen a concise code example that actually demonstrates this.
For instance, it's easy to demonstrate how Rust is superior to C: Just show a short piece of code where an array is returned from a function. In C, this will involve raw pointers and manual memory management with all associated safety and security pitfalls, whereas in Rust you just return a `Vec` and everything is taken care of. Simple, obvious, real-world superiority.
How does such an example for Lisp look like? I'd love to see 10 lines of Lisp that show me something that:
1. I can't easily do in, say, Rust.
2. Actually matters in practice.
- deleted 4y ago[deleted]
- eliben 4y agoI genuinely like Rust, but it's pretty amazing that you managed to write a comment on this article and make it about Rust. Peak HN :-) Re Lisp's superiority - Lisp was certainly a superior language when it was devised many decades ago, but over time much of its comparative advantage has been absorbed by other languages. Starting in the 90s when productive, GC'd scripting languages like Python started being prominent this trend accelerated. For a nice discussion of this see https://norvig.com/Lisp-retro.html https://norvig.com/Lisp-retro.html
- TurboHaskal 4y agoIs someone willing to go back in time and let comp.lang.lisp know about Rust?
- Sinidir 4y agoI mean i would but i don't know wether going back in time would pass the borrow checker.
- LanternLight83 4y agoI'd say the most satisfying experiences I have with lisp are a cross between clean abstractions and light code-golfing. To call out specific approaches, I'd highlight anaphoric macros, code-walking via (symbol-)macro-let [1], and the freedom to control when expressions evaluated (generally at read-time, compile-time, or run-time) for optimization [2][3]. [1]: https://letoverlambda.com/index.cl/guest/chap5.html#sec_ https://letoverlambda.com/index.cl/guest/chap5.html#sec_ [2]: Compiler macro chapters aren't free, but I guess read-time is covered here https://letoverlambda.com/index.cl/guest/chap4.html#sec_1 https://letoverlambda.com/index.cl/guest/chap4.html#sec_1 [3]: https://irreal.org/blog/?p=809 https://irreal.org/blog/?p=809 / https://web.archive.org/web/20140711171755/symbo1ics.com/blog/?p=1352 https://web.archive.org/web/20140711171755/symbo1ics.com/blo...
- jxy 4y agoJust in case people don't know about C. This is a short piece of code where an array is returned from a function. typedef struct {int v[4];} Vec; Vec get_vec(void){return Vec {3,2,1,0};} Edit: alas, I've been writing too much C++. The following is correct C. typedef struct {int v[4];} Vec; Vec get_vec(void){Vec v={{3,2,1,0}}; return v;}
- merlincorey 4y agoYeah, there is nothing complicated about returning a static array in C. C is a much simpler language than many people assume -- of course, it can get really hairy and complicated especially when you need to do dynamic memory management, but that's not what the GP was asking for in this case.
- chronial 4y ago> that's not what the GP was asking for in this case. You really think that when they mentioned the dynamically allocated `Vec` in Rust (which has fixed-sized arrays) and "manual memory management" in C, what they were actually talking about was a static array?
- jnxx 4y agoBut you need dynamic memory management for side-effect-free functional programming.
- xigoi 4y agoNow do an array whose size is not known at compile time.
- flohofwoe 4y agoIn C99 or later you can also write it as: Vec get_vec(void) { return (Vec){ { 3, 2, 1, 0 } }; } ...or I would probably prefer designated init to make the initialization a bit clearer: Vec get_vec(void) { return (Vec){ .v = { 3, 2, 1, 0 } }; }
- deleted 4y ago[deleted]
- jlarocco 4y agoConsidering Lisp was here first, shouldn't the real question be "why use Rust/C++/Python when there's Lisp?" You can't even create a real closure in Rust. I'd love to see 10 lines of Rust that showed me something that: 1. I can't easily do in Lisp. 2. Actually matters in practice.
- jahewson 4y agoThis a trick question because 10 line programs are trivial. Please show me a high performance hardware-accelerated 3D game engine written in lisp. Or a web browser.
- vzaliva 4y agoHow about IDE with base codebase of 1.5M loc and tons of 3rd part packages? (emacs)? Most modern programming is not about performance, it is about managing complexity.
- deleted 4y ago[deleted]
- japanuspus 4y agoEmacs? [0]: https://www.gnu.org/software/emacs/manual/html_mono/eww.html https://www.gnu.org/software/emacs/manual/html_mono/eww.html
- merlincorey 4y agoLisp has a rich history in games and graphics that you are not apparently aware of yet -- here's a few mostly Common Lisp Game Engines and resources to get you started (though GOAL was a Scheme variant): 1. https://en.wikipedia.org/wiki/Game_Oriented_Assembly_Lisp https://en.wikipedia.org/wiki/Game_Oriented_Assembly_Lisp 2. https://elliottslaughter.com/2011/06/blackthorn-3d https://elliottslaughter.com/2011/06/blackthorn-3d - Last updated in 2020 https://bitbucket.org/elliottslaughter/blackthorn-engine-3d/src/master/ 3. https://github.com/gregcman/sucle https://github.com/gregcman/sucle 4. https://github.com/BradWBeer/clinch https://github.com/BradWBeer/clinch 5. https://github.com/Shirakumo/trial https://github.com/Shirakumo/trial - This has games released on Steam built in it 6. https://www.cliki.net/game https://www.cliki.net/game 7. https://github.com/lispgames/lispgames.github.io/wiki https://github.com/lispgames/lispgames.github.io/wiki
- aidenn0 4y agoI can, without restarting my program: 1. Gather assembly level profiling information 2. Redefine & recompile a function 3. Gather new assembly level profiling I can iterate dozens of times between #2 and #3 in the time of a single incremental production build in rust (debug builds are not useful for gathering profiling data). Generally speaking it takes less than a second to recompile and load a source file, and it can be done while the program is running.
- jimbob45 4y agoDoesn’t VS do that too though? Also if you’re talking about performance or memory profiling, why are you using a Lisp in the first place?
- aidenn0 4y agoI haven't used VS in a decade, but you needed to restart your program after changing the code. Also, C++ linkage, while far faster than rust isn't the fastest thing, and compilation can be slow too (particularly with the popularity of header-only libraries).
- electroly 4y agoTen years ago, VS already had the "Edit and Continue" feature. But it's not very good compared to what you get in a Lisp environment. It's slow, there are restrictions on where/when it can be used, and it's possible for the edit operation to fail.
- jahewson 4y agoNo you don’t. Edit and continue in Visual Studio even works with C++ and it worked 20 years ago. It’s improved a lot recently.
- deleted 4y ago[deleted]
- NavinF 4y ago
- crdrost 4y agoI mean the famous example is of course, (define (eval exp env) (cond ((number? exp) exp) ((string? exp) exp) ((symbol? exp) (lookup exp env) ((eq? (car exp) 'quote) (cadr exp)) ((eq? (car exp) 'lambda) (list 'closure (cdr exp) env)) ((eq? (car exp) 'cond) (eval-cond (cdr exp)) (else (apply (eval (car exp) env) (eval-list (cdr exp) env))))) which uses a Lisp to define itself. This means roughly that if you understand enough Lisp to understand this program (and the little recursive offshoots like eval-cond), there is nothing else that you have to learn about Lisp. You officially have read the whole language reference and it is all down to libraries after that. Compare e.g. with trying to write Rust in Rust where I don't think it could be such a short program, so it takes years to feel like you fully understand Rust. Indirectly this also means that lisps are very close at hand for “I want to add a scripting language onto this thing but I don't want to, say, embed the whole Lua interpreter” and it allows you to store user programs in a JSON column, say. You also can adapt this to serialize environments so that you can send a read-only lexical closure from computer to computer, plenty of situations like that. Aside from the most famous, you have things like this: 1. The heart of logic programming is also only about 50 lines of Scheme if you want to read that: https://github.com/jasonhemann/microKanren/blob/master/microKanren.scm https://github.com/jasonhemann/microKanren/blob/master/micro... 2. Hygienic macros in Rust probably owe their existence to their appearance in Lisps. C2 asks the same question here: https://wiki.c2.com/?LispShowOffExamples https://wiki.c2.com/?LispShowOffExamples with answers like 3. The object model available in Common Lisp was more powerful than languages like Java/C++ because it had to fit into Lisp terms (“the art of the metaobject protocol” was the 1991 book that explained the more powerful substructure lurking underneath this object system), so a CL programmer could maybe use it to write a quick sort of aspect-oriented programming that would match your needs. 4. Over there a link shows how in 16 LOC you can implement a new domain-specific language to define and run finite state machines: http://www.findinglisp.com/blog/2004/06/automaton-cleanup.html http://www.findinglisp.com/blog/2004/06/automaton-cleanup.ht...
- Jtsummers 4y ago> 3. The object model available in Common Lisp was more powerful than languages like Java/C++ because it had to fit into Lisp terms (“the art of the metaobject protocol” was the 1991 book that explained the more powerful substructure lurking underneath this object system), so a CL programmer could maybe use it to write a quick sort of aspect-oriented programming that would match your needs. In addition to that, Lisps are sufficiently flexible that before CLOS itself was developed people were extending Lisp in Lisp to try out different object oriented models that fed into what became CLOS. That's hard to accomplish in most other languages, if it's even possible without going to a third party tool or digging into the compiler itself.
- yyyk2 4y agoAny sort of self-referential data structure, e.g. doubly linked lists.
- lelanthran 4y ago"For instance, it's easy to demonstrate how Rust is superior to C: Just show a short piece of code where an array is returned from a function. In C, this will involve raw pointers and manual memory management with all associated safety and security pitfalls, " Typedef the array, return that. One pitfall, it's not resizable.
- bayesian_horse 4y agoLisp is functional programming. You can leverage the functional programming paradigms to write more correct code. Also Lisp makes it easier to not repeat yourself. It's shorter to create functions, even macros, leading to the ominous "DSL" rabbit hole. Another superpower is that it has no predefined keywords or operators. So you can redefine everything to whatever unicode you want. Including other languages and writing systems. It's a lot harder to write a programming language/compiler that uses another natural language idiomatically using something more similar to C or Python.
- recuter 4y agoLisp is absolutely superior and you should absolutely learn it but you'll never get concise examples as to why. Only jokes and anecdotes. How do you fix a waterlogged smartphone? Put out a bowl of rice, which attracts an Asian guy who will repair it for you. Languages have pedigrees. If you pretend like your company is enamored with javascript you'll get people who love fedoras and call themselves Ninjas. Big teams, single function libraries, lots of code shipped - move fast and break things. Cats pawing at Macbook keyboards. Mumble rap. If you pretend you love Haskell you'll attract mathematicians in elbow patches. Great stable code will sporadically appear once every couple of years seemingly at random. Genius solutions to neat problems that have nothing to do with what the company is actually trying to accomplish. Ents. Classical music. If you pretend to love lisp you'll attract people who read PG essays and will quit to start their own companies. Maybe they'll help you close out some tickets in Jira before they bounce if they can get your Rube Goldberg monstrosity working on their laptop. Honey badgers and hamsters. U2. If you pretend to love latin you might get elected PM. If you actually learn a few orthogonal languages to cover the very finite amount of paradigms you'll eventually come to realize they are all crap. If you want to code, code. Don't talk. { ⊃ 1 ω ∨ . ∧ 3 4 = +/ +⌿ 1 0 ‾1 ∘.θ 1 - ‾1 Φ″ ⊂ ω }
- medo-bear 4y agoi can program my numerical-heavy program in SBCL with much better interactivity and debugging than python can offer and with a much much better performence as far as writing the actual code, lisp syntax allows me to perform structural editing which to me is just on another level but as with all things in life, you should try before you buy
- flohofwoe 4y ago> whereas in Rust you just return a `Vec` and everything is taken care of The downside is that you just performed a hidden heap allocation. If automatic memory management is desired, a garbage-collected language might have been the better choice in the first place.
- xigoi 4y agoHow would you do it without a heap allocation?
- flohofwoe 4y agoSince the OP wanted to return an array, the correct answer would probably be to return a simple native array, not a complex stdlib container: pub fn get_array() -> [i32;4] { return [1, 2, 3, 4]; } In C one needs to wrap the array in a struct type to work around the pointer decay: struct IntArray4 { int items[4]; } get_array(void) { return (struct IntArray4) { .items = { 1, 2, 3, 4 } }; } Both solution don't use heap allocation but pass around the array data by value on the stack or packed into register and should compile to the same assembly code (ABI differences aside). That's also one of the rare cases where Rust code actually turns out more simple and readable than the C equivalent ;)
- TurboHaskal 4y agoThere is something I saw on the wild here https://github.com/Shinmera/legit/blob/master/repository.lisp#L49-L56 https://github.com/Shinmera/legit/blob/master/repository.lis... which I thought was pretty cool. We all know about memoize, but let's say I want to define a global hash-map, where the keys are actual pieces of code and the value the result that would be evaluated when executed. Something like this: | Key | Value | |----------------------------+--------------------------------| | "hello" | "hello" | | (square 5) | 25 | | (factorial 5) | 120 | | (git-tags "~/foo") | (list "1.0.0" "0.1.0" "0.0.1") | | (make-instance 'singleton) | #<SINGLETON {1301DE5A03}> | Which can then be used like this in a trivial way, just passing code because code is data: (let ((path "~/foo") (tags (cache `(git-tags ,path)))) (format t "Tags of ~a~% ~{- ~a~%~}" path tags)) Sure, something like this is possible in other languages, but having done macros in languages such as Rust and Nim, it involves such a verbose and syntax soupy way of dealing with the AST that I don't feel like reaching for those abstractions that often. I'd rather just write boring code, and most consider this a feature.
- cb321 4y agoNim has `quote do:` with a kind of ghetto quasiquoting as well as genAST (and other things) to lessen the burden, but it is always simpler to write boring code (and better unless you have a burning need for The Fancy). One way to rephrase objections to "all those parens" of Lisp is that the most common style of using it makes it necessary to "write boring code 'in AST'", if you will, and not even in a very nice, commonly accepted 2-dimensional tree notation. I always wonder how different the history of prog.langs would be if early on one of the many indent/offside rule based 2-D notations had become popular with "boring code" writers in Lisp and not eschewed by "fancy macro writers" in Lisp.
- LanternLight83 4y ago> I always wonder how different the history of prog.langs would be if early on one of the many indent/offside rule based 2-D notations had become popular with "boring code" writers in Lisp and not eschewed by "fancy macro writers" in Lisp. That sounds interesting but is hard to search for, have you got an example?
- mmargerum 4y agoYou know all those DSL's you use to do templating, SQL, scripting? You don't need them in Lisp.
- coutego 4y agoWell, you need to think that LISP was a thing already before 1960. The only competitor at the time was FORTRAN. Even C was introduced more than 10 years later. LISP had garbage collection and was designed for symbolic manipulation. Given that programs were just list of symbols, it was fully meta, from the beginning. This gave it a raw power that was decades ahead of time. Even nowadays, this malleability give Lisp languages the power to provide as libraries things than in most languages would require the modification of the language itself. For example, there is library for Clojure (a popular modern Lisp which runs on the Java and Javascript virtual machines) that adds type support. Think about that. Yes, I know. Mypy adds types to Python, but in the case of Clojure the language and runtime didn't need to be touched at for the library to work. You can basically do whatever you want as a library, because the core of the language is so powerful. Another distinct characteristic of Lisps was the possibility to treat programs as living things you can just "talk to" through the REPL. Programs are developed in a more "conversational" way than with languages such as Java, Rust, etc. Some would say that this is a superpower and other would say that it's of marginal value. It just depends on the personal preferences. Now, we are in 2022. Obviously, languages have evolved a lot and they are ridiculously more advanced that FORTRAN. Lisps don't have a clear killer feature that can't be found in some other languages and actually they normally lack some convenient things. There is no type system for Lisps that it's practical, convenient and with tooling support. That is a big disadvantage on an era where programs are big beasts normally done by several people mostly gluing together a bunch of libraries with big APIs that you need to explore somehow. Typed languages with accompanying IDE tooling (i.e. having a language server) offer a much quicker and effective way to develop than spending the day reading API docs. Now, should you learn a Lisp in 2022? Well, I think there are some advantages of doing so. They have a lot of historical value and their simplicity and power are quite instructive, I'd say. Playing a bit with some Scheme (or Racket) can be very fun. If you are curious about Lisps and also functional programming, I'd suggest learning some Clojure. It's a very nice language and it really changes how you think about things, especially if you haven't been doing "hard" functional programming before. Clojure general approach and concrete libraries as Reitit, Malli or Specter really can change how you look at things and give you a deeper understanding of other characteristics of your other languages of choice. It's a bit like learning some Japanese if you are a German or French speaker. It can help you understand, for example, how unnecessarily complex your verbal system is and how unnecessarily complex Japanese numbering system is. If you are a fish, it's difficult to understand what water is unless you get out of it. Maybe Lisps can be this breath of fresh air.
- bluepoint 4y agoMy relatively amateur take is that the REPL and the debugging experience seem powerful. I would like to know how they compete with other languages. The REPL is the center of everything and it enables you to change functions on a running program. Tracing a function (shows function arguments on every call) is a simple as calling trace(function-name). If a program crashes, it does not really crash... it enters some debug mode which offers possible resolutions, including change the function that failed. The REPL can trivially show you the assembly code of individual functions. You can also add declarations for each function with hints for the compiler (and then check the size of the resulting assembly). I believe this series of articles highlights some of the debugging features. https://malisper.me/debugging-lisp-part-1-recompilation/ https://malisper.me/debugging-lisp-part-1-recompilation/ There is also a story called "debugging code from 60 million miles away". Other things that I did not manage to explore yet are the macros, which allows you to create your own domain specific languages. My general impression so far is that it is a really powerful language, for lonely hackers. :)
- mtreis86 4y agoI have no idea what you can or can't easily do in Rust. Here is something many languages can't do succinctly, without closures and code-as-data. From Paul Graham's book, On Lisp, modified to work with Lisp code as keys. (defun make-dbms (db &key (test #'eql)) "Make a database, db should be a list. Test determines what keys match." ;;Three closures in a list to make a database. (list #'(lambda (key) (rest (assoc key db :test test))) ;;add #'(lambda (key val) (push (cons key val) db) key) ;;delete #'(lambda (key) (setf db (delete key db :test test :key #'first)) key))) (defun lookup-dbms (key db) "Return the value of an entry of db associated with the key." (funcall (first db) key)) (defun add-dbms (key val db) "Add a key and value to db." (funcall (second db) key val)) (defun del-dbms (key db) (funcall (third db) key)) In use with code as the key: CL-USER> (let ((db (make-dbms nil :test #'equalp))) (add-dbms '(+ 1 2) 3 db) (lookup-dbms '(+ 1 2) db)) => 3
- kazinator 4y agoIn about one line of Lisp we can make an object with referential cycles in it, without declaring that we would like to abandon safety. We can have that object printed in a notation from which a similar object will be recovered, with the same cycles in the same places. All of this matters in practice.
- jnxx 4y agoLisp is both low-level and high-level at the same time. Common Lisp has more than the power of modern Python and Go combined, and the language is concise - it has about a tenth of Python's size. Just as examples, Common Lisp supports arbitrarily-long integers, low-level bitwise operations like popcount, rational and imaginary numbers as well as, say, POSIX file operations or easy calling into C functions. It has things like list comprehensions, pattern matching, and dictionaries, and full Unicode support since a long time. It supports both procedural and functional-style programming, and is, like Rust, a child of the language families which stem from the Lambda calculus, where everything is an expression. The latter is an extremely valuable property because you can replace any expression in lisp code with a function or its value, even if it is an if-statement. It has still facilities which other languages do not have, like built-in support for symbols, which are used similar to interned strings and keywords in Python. At the same time, Common Lisp is extremely mature. For example, it is possible to define and use error handlers, which is a generalization of exceptions, and is useful in library code. Or while other languages have only local and global variables, nothing in-between, Lisp allows to define parameters, which are global values, that however can be modified in the scope and call stack of a certain function call, similar as environment variables can be inherited and changed in sub-processes of a program. And it compiles to quite fast native code, which Python can't. Here is an introduction to Racket, which is a dialect of Scheme - I think it shows quite nicely the uniformity and simplicity of Lisps: https://docs.racket-lang.org/quick/ https://docs.racket-lang.org/quick/