Live data from Hacker News

An Intuition for Lisp Syntax

stopa.io

131–140 of 201 posts

Re: An Intuition for Lisp Syntax

#131

Can anybody recommend resources for the "next level"? I get that "code is data", the "unless" example is nice, but what I don't get is: why would I want to do that? As a non-LISP developer I get by without macros and all the fancy things that are possible with LISP. I would like to see a strong case of daily tasks that are much easier with LISP macros. Anybody?

If you want to learn a little bit about lisp, you might try some of Paul Graham's writing on the subject. http://www.paulgraham.com/lisp.html One thing you'll learn there is that it's possible to define a lisp interpreter, in lisp, in about a page of code.

But if you want to really appreciate macros, you'll need to read some books. There's a lot to learn.

My introduction to lisp was ANSI Common Lisp and On Lisp by Graham. On Lisp is all about macros. You need ACL to understand On Lisp. What you learn is pretty impressive -- I seem to recall one of the later chapters of On Lisp features a compiler for Prolog in two and a half pages of code -- but it requires a certain amount of supporting material. Still, if you want the most direct route to understanding (some of) lisp's greatness, these two would be it.

Another possibility is Paradigms of Artificial Intelligence Programming by Norvig. This one teaches you Common Lisp in the introduction, but I'm not sure it's enough by itself for you to really understand some of the later chapters. If you're prepared though (read ANSI Common Lisp first), this book is a gem. It's less about AI than about transforming and optimizing programs. So, code as data.

You might also look at Practical Common Lisp by Siebel. I haven't read this, but a lot of people liked it, and the code is very real-world (a little dated now, though). It's available online here: http://www.gigamonkeys.com/book/

In general, you wind up learning a couple of different lisps. Common Lisp and Scheme have the best literature, but the lisp that's most in use today is Clojure. Clojure's macro system is a refinement of Common Lisp's. For learning clojure there are a lot of teach-yourself-X-in-21-days type books. The best of them is the O'Reilly book: https://www.oreilly.com/library/view/clojure-programming/978...

Scheme doesn't (always) have macros, but I'd be remiss if I didn't suggest something. The Schemer books are some of the most effective pedagogy I have seen on any subject: The Little Schemer, The Seasoned Schemer and The Reasoned Schemer. They are very cute, but don't let that fool you. They get hard (in TLS's case maybe too hard) at the end.

Finally, there is one of the most important CS books of all time: The Structure and Interpretation of Computer Programs (SICP). It will change the way you think about programming forever. It also explains some important details about how lisp works, and it's so definitive that a lot of them aren't covered elsewhere. ("SICP already did that...") I reread my copy every five years or so, and I always come away knowing something new.

You don't need to read all this stuff to be a good lisp programmer. One or two of these would probably be enough. But I think it's important to have choices. If you want to talk in more depth, my email's in my profile.

Re: An Intuition for Lisp Syntax

#132
post #127
post #99

Earlier quoted context omitted.

> I could not help but thinking at the end, this is really awesome but s-expressions are kind of hard to read and reason about for my brain is (map foo '(1 2 3)) that more difficult than: [1, 2, 3].map(foo) ? Or: (let ((array #(1 2 3 4 5))) (vector-set! array 0 3) (vector-ref array 0)) that more difficult than: let array = [1, 2, 3, 4, 5]; array[0]=3 array[0]

Is (* (f (+ 2 x)) 5) more difficult to parse than: 5 * f(x+2) Considering that we're familiar with the latter syntax (more or less) since kindergarten, I'd say... yes. And the idea that you'd somehow start to consider something you've been doing all your life as “irregular” after 2–4 weeks of Lisp cure is just hilarious.

>And the idea that you'd somehow start to consider something you've been doing all your life as “irregular” after 2–4 weeks of Lisp cure is just hilarious.

And yet we learn all kinds of syntax that we haven't been familiar at all (zero indexing, x=x+1, etc which we used to the exact inverse: math assignments being immutable or denoting an equation not increment, etc),

-- not to mention advanced stuff like generics, futures, closures, etc --

and we seem to manage just fine...

Re: An Intuition for Lisp Syntax

#133

Can anybody recommend resources for the "next level"? I get that "code is data", the "unless" example is nice, but what I don't get is: why would I want to do that? As a non-LISP developer I get by without macros and all the fancy things that are possible with LISP. I would like to see a strong case of daily tasks that are much easier with LISP macros. Anybody?

They are useful to refactor code, make it more terse, and thus ease one's life. For example, I am writing API endpoints. Each route uses the same scheme, they need to hold a list of messages with their status code which will be sent to the user. In Python, I must currently repeat their declaration in each function. Boilerplate. What should I do, use classes? Ugly solution. A quick `with-my-variables` macro would write the boilerplate for me, ensuring it is written at one place, making it easier for maintenance, future refactoring, etc.

Python has decorators. They have a specialized, limited interface. Sometimes they are limiting you. A macro would impose no limit.

Re: An Intuition for Lisp Syntax

#134
post #130

Earlier quoted context omitted.

I'm not sure who GJS is but if you see any lisper editing text instead of operating on structures (with auto-balancing parenthesis and so on), it's 99% certain it's in an environment they are not familiar with, so they will make mistakes. I don't think anyone who write Lisp-like languages professionally doesn't use tools like parinfer/paraedit, where balancing parenthesis is not something you have to do.

I'm pretty sure there are people who write common lisp professionally whithout those tools. Are they even available in the LispWorks or ACL editors?

While specifically parinfer and/or paraedit might not be available in neither LispWorks or ACL editors, they surely have something there to aid with parenthesis.

But I might be wrong here, it's simply hard for me to imagine someone writing with lisps professionally going the route of textual editing when structural editing is right there.

Re: An Intuition for Lisp Syntax

#135

Can anybody recommend resources for the "next level"? I get that "code is data", the "unless" example is nice, but what I don't get is: why would I want to do that? As a non-LISP developer I get by without macros and all the fancy things that are possible with LISP. I would like to see a strong case of daily tasks that are much easier with LISP macros. Anybody?

I think a good candidate for macros is anything you would do with Annotations in Java or similar languages. Macros are a much cleaner and easier to reason about way of doing code generation than whatever it is Java annotation processors do to generate code.

And in the same way you should think twice about introducing a new code of annotation to your program, you should think twice about adding a macro to a Lisp program.

But boy, when you really need it, it can really be a life saver.

Re: An Intuition for Lisp Syntax

#136

This is maybe the best introductions to Lisp i have seen, especially for a js dev like me it could hardly get more approachable and convincing. I could not help but thinking at the end, this is really awesome but s-expressions are kind of hard to read and reason about for my brain, it would be cool if we could generate them from more readable syntax, maybe something like javascript :D . Unfortunately the standard js…

> I could not help but thinking at the end, this is really awesome but s-expressions are kind of hard to read and reason about for my brain, it would be cool if we could generate them from more readable syntax, maybe something like javascript :D

Little known fact: The people who came up with lisp wanted to do this, too.

https://www.wikiwand.com/en/M_expressions

Re: An Intuition for Lisp Syntax

#137
post #37
post #16

So what does Lisp make harder to implement than today's programming languages? It seems like large-scale composition relies on a well-structured way to define and enforce APIs, and oh wow, typing facilities. Performance has historically been an issue for Lisp because its model is tightly tied to interpretation, but perhaps modern JIT-style compilation can address this. Others? What type of issues do large Lisp projec…

> a well-structured way to define and enforce APIs I don't see the issue here? What do you think is stopping you from doing this in, for example, Common Lisp? > and oh wow, typing facilities Common Lisp isn't typed, but there's no reason a Lisp dialect can't be. In fact, Typed Racket is just such a language.

Common Lisp is typed. It is not (consistently, see SBCL for a counterexample) statically typed. If you try to do:

  (+ "hello" 3)
You will get an error in CL, and with SBCL an expression like that wouldn't even compile.

Re: An Intuition for Lisp Syntax

#138
It's not enough to only talk about syntax when we introduce Lisp.

The other less known Lisp feature (in the sense of Common Lisp), is the image-based development, which makes for a great REPL. Really interactive, tiny feedback loops, where you compile your program one function at a time. You never wait for a process to restart, even working on a web, GUI or game project. You keep your test objects around. You restart to a clean state when you want. You get compilation and type warnings immediately. You have an interactive debugger that points you to the erroneous line, you fix it, you tell the debugger to try again from the previous stackframe, and you see the function complete.

Explore! https://github.com/CodyReichert/awesome-cl

Re: An Intuition for Lisp Syntax

#139
post #8
post #7

IMO, by far the most confusing thing about lisp syntax is not code data duality, but instead that when you actually dive into the specs for these languages, its actually still really complicated and full of edge cases. A "toy" lisp can be simple sure, but "real" lisps aren't.

The formal Scheme syntax spec is, what?, a few pages. Far smaller than any other "real" language I'm aware.

I enjoy lisp/scheme syntax, but I'm especially impressed by lua's simple, readable, flexible, whitespace-insensitive-but-whitespace-friendly syntax. It is a bit more complicated than scheme's, but more intuitive because you don't need to explain quasi-quoting and other lisp-specific stuff. https://www.lua.org/manual/5.4/manual.html#9

Re: An Intuition for Lisp Syntax

#140
post #130

Earlier quoted context omitted.

I'm pretty sure there are people who write common lisp professionally whithout those tools. Are they even available in the LispWorks or ACL editors?

While specifically parinfer and/or paraedit might not be available in neither LispWorks or ACL editors, they surely have something there to aid with parenthesis. But I might be wrong here, it's simply hard for me to imagine someone writing with lisps professionally going the route of textual editing when structural editing is right there.

Something, but probably not as much as you think:

http://www.lispworks.com/documentation/lw71/EDUG-M/html/edus...

http://www.lispworks.com/documentation/lw71/EDUG-M/html/edus...

Post reply on HN