News for hackers. Stuff that matters.
Re:Show HN: Full Stack Lisp – A book in progress about writi... by avmich on Thursday June 02, 2016 @02:02PM (#11824660)
Can you include a good description of reading macros? I'm still looking for good explanation with examples of their power.
Re:Show HN: Full Stack Lisp – A book in progress about writi... by martinflack on Thursday June 02, 2016 @03:30PM (#11825354)
You probably mean reader macros. Here are a few resources:

Hyperspec reference for predefined sharpsign reader macros: http://www.lispworks.com/documentation/HyperSpec/Body/02_dh....

A blog post where Vsevolod Dyomkin creates a reader macro to notate list comprehensions to his liking: http://lisp-univ-etc.blogspot.com/2013/01/real-list-comprehe...

A blog post where Inaimathi mentions using CLSQL reader macros to facilitate reading SQL expressions in code: http://langnostic.blogspot.com/2011/03/clsql-and-nothing-els...

Let Over Lambda chapter 4 which has various examples: http://letoverlambda.com/index.cl/guest/chap4.html

Re:Show HN: Full Stack Lisp – A book in progress about writi... by junke on Thursday June 02, 2016 @02:16PM (#11824800)
A reader macro is not a kind of macros [0].

It is a way to customize the Lisp reader by associating a custom function to an entry in the readtable.

So you can say that character '[' will be tied to a custom reader function, which reads from a stream and produces a Lisp object, at readtime.

The standard sharpsign[1] character is used to read different kind of structures: complex numbers #C(0 1), structs #S(tree :left nil :right nil), and so on.

In particular, you can have sharpsign-dot, which reads a lisp form, evaluates it at read time; the result is the form being read.

Also, you can use #1= and #1# to affect parts of your code to reader variables and reuse them in other places.

If the PRINT-CIRCLE (there are earmuffs around it, but I can't format it here) variable is set to true, here is a quine:

     #1=(print '#1#)
[0] http://www.lispworks.com/documentation/HyperSpec/Body/26_glo...

[1] http://www.lispworks.com/documentation/HyperSpec/Body/02_dh....

Re:Show HN: Full Stack Lisp – A book in progress about writi... by baggers on Thursday June 02, 2016 @05:07PM (#11826069)
I found them useful when I looked at clojure and saw their shorthand syntax for lambdas where #(* % %) is a lambda that takes one argument and squares it. You can get some tidy things that are similar with regular macros but to make it actually equivalent your need to hook into the reader..and that's what reader macros are for. So within the hour I had something that let me write λ(* _ _)

The best bit though, is that this isn't some nasty hack. This is supported by the spec, so I can package this up and let other people us it just like any other functionality we care to ship around.

That's my favorite thing really, being able to treat approaches to writing code in the same way we treat the functionality we make using code.

Re:Show HN: Full Stack Lisp – A book in progress about writi... by kazinator on Thursday June 02, 2016 @05:56PM (#11826423)
> That's my favorite thing really, being able to treat approaches to writing code in the same way we treat the functionality we make using code.

You mean ... programming the programming language?

> I can package this up ...

Not really; it will clash with someone else's use of λ. Read macros do not "package" particularly well. (Racketlang has a good solution for this: you put a #lang whatever directive at the top of a file and then the rest of the file uses the special syntax associated with that lang name).

It's very easy to overestimate the usefulness of read syntax.

This feature of Common Lisp is used far less than newcomers might imagine, though there are some famous examples of it. If you randomly sample Lisp code, you are much more likely to find a macro than a read macro.

> So within the hour I had something that let me write λ( _ _)*

Do you plan to type "lambda" with args enough times to eventually that hour of your life back? Ha.

Useful syntax for writing anonymous functions can be had, without relying on read macros at all.

cl-op: https://code.google.com/archive/p/cl-op/

Note how if we take λ(* _ _) and then just move the parenthesis over the Greek symbol, we get (λ * _ _). This saves almost the same amount of typing. If your Lisp recognizes λ as a symbol token, then all you have to do now is write a regular macro. Basically, just get the cl-op package and then set up λ as an alias for op: (defmacro λ (&rest args) `(op ,@args)).

In TXR Lisp, I implemented a more powerful op which has numbered arguments, and an escape syntax for inner ops to refer to outer ops.

Y combinator with TXR Lisp op:

  ;; The Y combinator:
  (defun y (f)
    [(op @1 @1)
     (op f (op [@@1 @@1]))])

  ;; The Y-combinator-based factorial:
  (defun fac (f)
    (do if (zerop @1)
           1
           (* @1 [f (- @1 1)])))

  ;; Test:
  (format t "~s\n" [[y fac] 4])
Combinators return us to your topic of writing a function which squares its argument. Instead of a condensed lambda notation like (op * _ _) for doing this with syntax, we can have a combinator, like (dup #'). This is nicer in a Lisp-1 language where we can just do (dup ). Dup takes its argument, which is a two-argument function, and returns a one-argument function which calls that function with two copies of the argument:

  (defun dup (fun)
    (lambda (arg) (funcall fun arg arg)))

  (funcall (dup #'*) 2) -> 4

  (mapcar (dup #'*) '(1 2 3)) -> (1 4 9)
Re:Show HN: Full Stack Lisp – A book in progress about writi... by baggers on Friday June 03, 2016 @03:24AM (#11828865)
Hehe, what are curiously peeved sounding post. I was seeking to keep it short but as I've been unclear let's drill into some stuff.

> Not really; it will clash

Nope, named-readtables fixed the clashing reader-macro issue a good while back.

> You mean ... programming the programming language?

Yes, I do. I said it the way I did out of choice, as the 'programmable programming language' phrase has been bandied around so often that, for some, it has lost impact. Maybe that phrase doesn't inspire the same ahah moment in some as it does for other. Saying the same thing in a different way can be enough for someone to pick up something new. (It feels weird writing this down as it's kind of self evident)

> Do you plan to type "lambda" with args enough times to eventually that hour of your life back

I that hour was spent learning about reader macros, learning something new is of value to me, so there is no 'lost hour' to reclaim. Don't you just code for fun some days, without worrying if it is somehow ultimately useful?

> Note how if we take λ(* _ _) and then just move the parenthesis over the Greek symbol, we get (λ * _ _). This saves almost the same amount of typing

Ah ok, maybe this is where we parted mental ways. I'm not interesting in saving typing, that wasn't the goal. The point was to remove a little visual obstruction from what I was doing. For example (and yes it is a trivial example) (lambda (x) (* x x)) is 20 characters long, of which 8 are of interest (* x x) I liked that the shorthand reduced a little visual noise.

Yes there are many ways to skin this particular cat, but I liked the shorthand. I also like not having the lambda in the first position as it maintained a property of lisp code I generally enjoy, that I can run my eyes over the lefthand side of the forms and quickly get a rough feel of what is going on. Again, this is just a personal feeling, but some days I write code just for me, for fun, so why not make things pleasant for myself.

It's rather odd to get into a discussion about this, when the whole point is that we don't have to pick one golden way and enshrine it in the language. It's just a library, use it or don't, it doesn't matter.

Peace

Re:Show HN: Full Stack Lisp – A book in progress about writi... by aidenn0 on Thursday June 02, 2016 @09:54PM (#11827776)
named-readtables has largely solved the packaging issue of read macros.
Re:Show HN: Full Stack Lisp – A book in progress about writi... by EuAndreh on Friday June 03, 2016 @03:06AM (#11828809)
Yep, named-readtables do a pretty good job on packaging read-macros. You've probably missed it.
Re:Show HN: Full Stack Lisp – A book in progress about writi... by kazinator on Friday June 03, 2016 @09:36AM (#11830206)
Packaging means "better than just loading something and having it pee in the global readtable". It doesn't solve the problem that read macros compete for the same characters and clash.

Documentation for merge-readtables-into:

"If a macro character appears in more than one of the readtables, i.e. if a conflict is discovered during the merge, an error of type READER-MACRO-CONFLICT is signaled."

Re:Show HN: Full Stack Lisp – A book in progress about writi... by aidenn0 on Friday June 03, 2016 @11:45PM (#11834814)
Exactly, and this is awesome. If there is a conflict, I get an error and can decide what to do.

At their core, reader-macros are abbreviations. Obviously there are a finite number of abbreviations one can use, so any time you want to add concise syntax from more than one source, there is a chance of conflict.

Re:Show HN: Full Stack Lisp – A book in progress about writi... by kazinator on Saturday June 04, 2016 @09:58AM (#11836333)
I can see how this would have value if your idea of Lisp programming is to bring half a dozen new read syntaxes, and use them all in the same source file. Plus keep up with upstream enhancements in all of them, so you have to guard against new clashes popping up.
Re:Show HN: Full Stack Lisp – A book in progress about writi... by xenophonf on Thursday June 02, 2016 @02:11PM (#11824740)
Do you mean reader macros? Those are used to implement new kinds of syntax within Common Lisp's parser, the READ function. Even standard parts of Common Lisp syntax are defined this way, such as quote (') or backquote (`).

For more examples, see http://lisper.in/reader-macros/.

Re:Show HN: Full Stack Lisp – A book in progress about writi... by aidenn0 on Thursday June 02, 2016 @09:56PM (#11827778)
Actually the entirety of Common Lisp's parser except for numbers, symbols, and the consing dot are implemented as read macros.
Re:Show HN: Full Stack Lisp – A book in progress about writi... by xenophonf on Thursday June 02, 2016 @10:58PM (#11828001)
It's been a while since I looked at the spec, but I think the dot as used in cons cells is part of the left-parenthesis reader macro.
Re:Show HN: Full Stack Lisp – A book in progress about writi... by aidenn0 on Friday June 03, 2016 @01:18AM (#11828491)
Re:Show HN: Full Stack Lisp – A book in progress about writi... by xyience on Thursday June 02, 2016 @03:56PM (#11825557)
A diagram helps when you start getting into read/compile time discussions, see slides 13 through 17 of http://www.slideshare.net/adorepump/clojure-an-introduction-... to see how the evaluation model for Clojure (which is similar enough to Lisp) differs from most other languages. One of the many differences between Clojure and Lisp though is that Lisp supports user-defined reader macro definitions, so you can add things like the nice persistent map literal syntax of Clojure to Lisp if you really want to.
Re:Show HN: Full Stack Lisp – A book in progress about writi... by sedachv on Monday June 06, 2016 @05:39PM (#11850631)
Read macros are great because you can use them to turn the Common Lisp parser into a parser for any language you want (no restrictions on the grammar), so you essentially get compilers for free. I wrote a one-pass C preprocessor and parser in just under a thousand lines of code: https://github.com/vsedach/Vacietis/blob/master/compiler/rea...
Re:Show HN: Full Stack Lisp – A book in progress about writi... by jonathankoren on Thursday June 02, 2016 @10:15PM (#11827837)
It's just a macro. It's pretty much exactly like a #define in C
Re:Show HN: Full Stack Lisp – A book in progress about writi... by _ph_ on Friday June 03, 2016 @05:07AM (#11829158)
This is pretty much wrong. C macros have a simple substitution language. But all Common Lisp macros can run arbitrary common lisp functions to compute the macro expansion. That means, you are not limited to pattern matching, but can write a program - in the same programming language you are using for your application - to compute the expansion. Of course there are simple cases which are just syntax rewrites, but there are also complex cases, like the loop macro. The loop macro allows you to specify looping constructs in an almost natural language.
Re:Show HN: Full Stack Lisp – A book in progress about writi... by junke on Friday June 03, 2016 @01:20AM (#11828493)
They only share the name.