I say all this because I would really like some great examples and use cases which would sort of highlight what LISP has to offer over the, for the lack of a better word, non-LISP languages. All of the articles that pop up in hacker news or other portals only seem to highlight the fact that LISP presents a new paradigm (?), enhances intuition (?), and that LISP macros are the best-thing ever. I would like to share the sentiment but ideally via a few digestible examples in lieu of a serious commitment required to basically learn an entire language. I would appreciate a few concrete examples (very much like [::-1] and 100**100) that would allow me to get what LISP really is about.
Roman: what's so amazing about the decimal system? I can do addition just fine with X + X = XX.
Modern human: Well... you can do negative numbers easily, irrational numbers, you can show zero easily.
Roman: bah! I don't know what you're talking about. Half those numbers don't even exist.
Mathematician: actually have you heard about complex numbers?
The point is: you have to learn a new way of thinking compared to procedural/OOP style programming. And it might be the case that the problems it solves are not practical for your use-case.
I tell you, as a non-Lisp programmer, what I find amazing about Lisp. Disclaimer: I may be partially or completely wrong. I'm really not the one who should be writing this, but since our knowledge of Lisp isn't that far apart, I might be able to empathize with your perspective a bit more since I have been in it.
* Your code is the abstract syntax tree! Don't know what an abstract syntax tree is? It's a data structure that compilers use in order to generate code. In normal languages you have to first parse the language, compile it to the abstract syntax tree and then generate machine code from it. However, in the case of Lisp, it's already there. So this means that you can write compiler-esque programs much easier.
* One of the nice things that a direct AST provides that it allows partial live reloading of code. Imagine this:
``` myAge = 17 if (myAge >= 18) { print("Nice!") } else { print("Hello youngster!") } ```
With Lisp, you can basically swap out the first print function for another function while the program keeps running.
One simple example I can describe is from the book Practical Common Lisp and it is talked about to some degree in the SICP videos. The idea of the environment being captured as well as what it points to is a big idea. At the risk of not getting it explained 100% to everyone's satisfaction I would suggest reading about it.
I also would suggest the problem of the N queens board. It is a pretty good starting point into how this problem can be solved in lisp. I suggest the MIT SICP lecture on it because it is well paced and interesting. Also it shows off some, for the time, advanced language features.
Another good read is Norvig's PAIP book that you can read on Github. Lots of problems you can jump to with pretty good explanation in my opinion.
I'm sure there are more interesting things I could point to. However, I still consider a read of Practical Common Lisp a good starting point. The book is fairly well paced and not bogged down with learn all the language first-isms. Also, the SICP videos are pretty good to watch. I hope these points are helpful to you.
useful link:
https://github.com/norvig/paip-lisp
https://emacs.stackexchange.com/questions/5465/how-to-migrat...
You mean like (reverse "asd") and (* 10000 10000)? That's not really what gives you an impression of Lisp, rather a bit of syntax ([::-1], the paragon of readability) and large integers. :P
This article might help: https://www.defmacro.org/ramblings/lisp.html
Also, if you really want to see what it's about you can read SICP: https://xuanji.appspot.com/isicp/ https://github.com/sarabander/sicp
Will you be able to understand how to play the guitar if I show you a passage from a song ? You can play it with any other instrument, so it still won't be motivating enough for you to put in the time and effort required to learn it.
If I were to take you to a Led Zeppelin concert to see Jimmy Page playing, that still might not be motivation enough for you to pick up the guitar, but you'll be able to better understand what it can do to people :).
Back to LISP - it's a tool. The difference between other tools, is that you can program it too. The power lies in its simplicity and your ability to build programs iteratively while running them.
For a while in the development of Common Lisp, a sort of joke acceptance test for implementations was in three parts: 1) you type T and enter into the REPL, it responds T 2) you define the factorial function and calculate (/ (factorial 1000) (factorial 999)) and it responds 1000 3) You try (atanh -2) and if it returns a complex number it passes (extra credit for the correct complex number). Lisp has a great numerical tower. Besides being able to deal with huge and complex numbers, you also have convenient syntax for specifying numbers in base 2 and 16, and you can do things like bitwise operations on bit-vectors:
(bit-and #*00110100
#*10101010)
-------> #*00100000
For macros, there's a lot of cool ones. Unlike C macros that just do string substitution, Lisp macros let you use the full Lisp language to write code that does something with expressions passed to the macro. A nifty one that's part of basically every implementation with uiop:nest is described here https://fare.livejournal.com/189741.html The problem it solves is that sometimes in Lisp you'll have a lot of nested forms, and your code begins to attack the bottom right corner of your screen. e.g. (multiple-value-bind (a1 b1 p1) (foo1)
(with-open-file (f1 p1 ...)
(let ((x1 (read f1)))
(when x1
(multiple-value-bind (a2 b2 p2) (foo2)
...
The macro lets you rewrite that as (nest
(multiple-value-bind (a1 b1 p1) (foo1))
(with-open-file (f1 p1 ...))
(let ((x1 (read f1))))
(when x1)
(multiple-value-bind (a2 b2 p2) (foo2))
...
The macro definition is really straightforward: (defmacro nest (&rest r)
(reduce (lambda (o i) `(,@o ,i)) r :from-end t))
(The list of forms are passed, unevaluated and at compile time, to nest, which rewrites them using a right fold to nest things properly.)Somewhat similar is the arrow macro that Clojure popularized, which lets you get rid of (deep (nesting (like (this ...)))) where you have to remember evaluation order is inside-out and replace it with a flatter (-> (this ...) like nesting deep). Or (loop (print (eval (read)))) -- which will indeed give you a primitive REPL within Lisp -- with the more readable (-> read eval print loop). Its implementation is also easy -- many macros are easy to write because Lisp's source code is itself a list data structure for which you can write code to process and manipulate just like any other lists.
Another cool macro that's been around since 1993 is https://github.com/quil-lang/cmu-infix which lets you write math in infix style, e.g. #I( C[i, k] += A[i, j] * B[j, k] ) where A, B, and C are all matrices represented as 2D arrays. It's a lot more complicated than the nest macro, though.
There are some other things that still make Lisp great in comparison to other languages, but they don't exactly have one-line code examples like [::-1] and so I'll just describe them qualitatively. Common Lisp has CLOS, the first standardized OOP system. It's a lot more powerful than C++'s system. It differs from many systems in that classes and methods are separate; among other things this gives you multiple dispatch (you can define polymorphic methods that don't just dispatch to different code depending on the first argument (the explicit 'self' in Python, implicit 'this' in other langs) but all arguments). One thing it can be useful for is to get rid of many laborious uses of the Builder and Visitor patterns. e.g. the need for double dispatch is a common reason to use the Visitor pattern, but in Lisp there's no need. CLOS also does "method combination", which lets you define :before, :after, and :around methods that operate implicitly before/after/around a call. This gets rid of the Observer pattern, supports design-by-contract, and jives well with multiple inheritance in that you can create "mixins" that classes can "inherit" from with the only behavior being some :before/:after methods. (e.g. logging, or cleaning up resources, or validation.)
Everything is truly dynamic -- an object can even change its type at runtime, which may be an acceptable solution to the circle-ellipse problem, or just super convenient while developing. More fundamentally, "compile" is a built-in function, not something you have to do with a separate program. "Disassemble" is built-in, too, so you can see what the compiler is doing and how optimized something is. You have full flexibility to define and redefine how your program works as it's running, no need to restart and lose state if you don't want to. Besides being killer for development (and all the differences in development experience comprise a big part of why I still think Lisp is great compared to non-Lisp), this gives you a powerful way to do production debugging and hot-fixing too -- a footgun you might not necessarily want most of the time, but you don't have to do anything special for it when you do want it. It can be very useful, e.g. if you've got a spacecraft 100 million miles from Earth https://flownet.com/gat/jpl-lisp.html I've also put some hobby stuff on a server, just deployed as a single binary, but built so that if I want to change it, I can either stop it, replace the binary, and start again, or just SSH in and with SSH forwarding connect to the live program with my editor and load the new code changes just like I would when developing locally, and thus have zero downtime.
Lastly, Lisp's solution to error handling goes beyond traditional exception handling. Again this ties into the development experience -- you have some compile-time warnings depending on the implementation (e.g. typos, undefined functions, bad types) but you'll hit runtime errors eventually, Lisp provides the condition system to help deal with them. It can be used for signaling non-errors, which has its uses, but what you'll see first are probably unhandled errors. By default one will drop you into a debugger where the error occurred, the stack isn't immediately unwound. Here you can do whatever -- inspect/change variables on different stack frame levels, recompile code if there's a way to fix things, restart computation at a specific frame... You'll also be given the option of "restarts", which might include just an "abort" that unwinds to the top level (possibly ending a thread) but can include custom actions as well that could resolve the error in different ways. For example, if you're parsing a CSV file and hit a value that is wrong somehow (empty, bad type, illegal value, bad word, whatever), your restarts might be to provide your own value or some default value (which will be used, and the computation resumes to parse the next value in the row), or skip the whole row (moving on to the next one), or skip the whole file (moving on to the next file, or finishing). Again this can be very useful while debugging, but in production you can either program in default resolutions (and a catch-all handler that logs unhandled errors, as usual) or give the choice to the user (in a friendlier way than exposing the debugger if you please).