News for hackers. Stuff that matters.
Re:A Year in Clojure by ilteris on Sunday October 07, 2018 @10:26AM (#18160682)
I would love to read more about "code as data" in general. I would appreciate any resources you can provide. Thanks.
Re:A Year in Clojure by chongli on Sunday October 07, 2018 @11:34AM (#18161052)
I took a course in Racket in first year (required for all first year math/CS students at my school) and I've spent some time in the Clojure community as well. "Code as data" is an oft-repeated selling point of Lisp and Lisp-like languages.

When you actually start using these languages, you find yourself tempted to leverage this property and try writing lots of macros. But then you go back to the community and everyone tells you "the first rule of macros is you don't write macros." It turns out that if you use macros all over the place then your code becomes impenetrable. The problem is that macros allow you to circumvent the expected order of evaluation and produce your own novel syntactic structures. This puts lie to the old claim "Lisp has no syntax." In reality, Lisp has tons of syntax, it's just informal and buried within the definitions of macros.

It parallels the problem put forth in Jo Freeman's famous piece, "The Tyranny of Structurelessness" [1]. Any group that purports to adopt a structureless organization ends up having a hidden, informal structure. This is the way of Lisps as well.

[1] https://www.jofreeman.com/joreen/tyranny.htm

Re:A Year in Clojure by agumonkey on Sunday October 07, 2018 @04:22PM (#18162443)
There's also one very easy to understand part of the 1st rule. A macro is still a piece of logic, and functions do that very well, so do as most as you can as functions at the metalevel so you can enjoy ease of debugging and everything else; then coat it with macro sugar.
Re:A Year in Clojure by lvh on Sunday October 07, 2018 @12:12PM (#18161263)
Clearly languages have common idioms. Lisp just has most of them implemented in terms of very basic idioms. Saying “macros are bad because you can overdo it” is a little silly. Sure: prefer plain old data manipulation over syntax manipulation. But when you actually want syntax manipulation, it’s pretty darn nice.

Equivalent arguments exist for Ruby. Ruby isn’t bad because someone wrote a DSL that should’ve been a method and maybe a hash.

Re:A Year in Clojure by ech085 on Sunday October 07, 2018 @12:49PM (#18161469)
That was a fantastic read, so much insight into group dynamics!
Re:A Year in Clojure by moocowtruck on Sunday October 07, 2018 @12:11PM (#18161257)
i think https://www.cs.kent.ac.uk/people/staff/dat/miranda/wadler87.... 'why calculating is better than scheming' also makes this point too; i'm going to check out this jofreeman thing... never saw it before thanks!
Re:A Year in Clojure by lvh on Sunday October 07, 2018 @12:15PM (#18161274)
The author is one of the principal folks behind Haskell. But I can’t help but point out that I promise you you’ve never heard of the five or so lanaguages he mentioned as an alternative approach, and somehow this Lisp thing is still around. I suppose that which is dead may never die :-)
Re:A Year in Clojure by dragonwriter on Sunday October 07, 2018 @06:54PM (#18163067)
> But I can’t help but point out that I promise you you’ve never heard of the five or so lanaguages he mentioned as an alternative approach

I actually had heard of at least KRC and Miranda even before the first time I've read the paper, but even if I hadn't I would immediately be able to think of several languages with some or all of the additional features over the Lisps he was comparing them to for which he describes them as superior to Lisp for the purpose, including some modern Lisp derivatives.

Pattern matching, mathematical notation, and static typing with use defined types are all features that have become more, not less, common in languages since the paper was written.

Re:A Year in Clojure by Volt on Monday October 08, 2018 @07:54PM (#18172299)
To be fair, Haskell is the Common Lisp of lazy functional languages. You don't hear much about Maclisp or Zetalisp anymore either.
Re:A Year in Clojure by malloryerik on Sunday October 07, 2018 @06:29PM (#18162963)
Was the Racket course based on How to Design Programs[1]? How was it?

[1]https://htdp.org

Re:A Year in Clojure by chongli on Sunday October 07, 2018 @06:41PM (#18163015)
Yes, it was! The course is called CS 135: Designing Functional Programs [1]. I really enjoyed it, though I think most of the first years hated the course. Most people come to school having experience with imperative languages like Python and Java. When they got to recursion in Racket they really struggled!

I happen to own a copy of HTDP which I bought for the course. It's a suggested textbook but not required at all. I haven't had time to go through it though.

[1] https://www.student.cs.uwaterloo.ca/~cs135/

Re:A Year in Clojure by lvh on Sunday October 07, 2018 @11:17AM (#18160954)
Sibling comment provides a great resource, but I think a good place to start is explaining macros.

At the end of the day, every line of code you write is some data in a bespoke data format. Most programming language implementations leverage this, and academic compiler courses make you do the same work. A program lexes, parses, modifies... that data structure and eventually rewrites it to some other programming language, usually something lower level.

If you exposed that underlying structure to the programmer, you give them a lot of power! The simplest way to see this is that any time you write a highly repetitive bunch of code, wouldn't it be great if you could ... write some code that wrote the code for you? You know how to systematically express the thing you want: you can probably say it a lot more crisply in a few lines of (especially functional) programming languages.

Once you get rid of simple repetitive stuff, you realize that you can do so much more with this. You want advanced pattern matching? Sure: you can go implement it as a library. async/await? Library. Type checker? Library. New convenient way of defining functions? Library. New syntax for matrix math? Library. Et cetera :-)

People in other programming languages have figured this out too. That's why we have code generators. The difference is that code generators are far enough removed from what you're actually doing (because e.g. they're glorified string concatenation) to be quantumly less useful. Generally speaking advanced IDEs will do this sort of thing: actually parse the code you're working on and let you do fancy tricks with it. Lisp is like having that power, all of the time.

Re:A Year in Clojure by travisoneill1 on Sunday October 07, 2018 @11:27AM (#18161013)
I messed around with Clojure for a couple of days to see what a Lisp is like but never got into any of the advanced features. What could I build that would illustrate the capability you are talking about?
Re:A Year in Clojure by lvh on Sunday October 07, 2018 @11:42AM (#18161099)
It's hard for me to provide a good example without knowing some of the stuff you're in to but here's one of my favorite macros: https://github.com/lvh/caesium/blob/master/src/caesium/bindi...

It takes a symbol and figures out the name of a C function, how to most efficiently call it (with type hints for perf). If you screwed that up in one location it might have security consequences. But I _can't_ scre it up, because instead of copy pasting that code a gazillion times as I would have in Python, I just figured out what I want once and then use that functionality a gazillion times :)

Re:A Year in Clojure by eval on Sunday October 07, 2018 @01:12PM (#18161586)
As an exercise you could do on a REPL what a macro would do. Say you have a file containing “(1 + 1)”. Reading this (using slurp and read-str) would yield a list, eval-ing this list would however fail as it’s not valid Clojure. To make it eval-able you transform the list (all the operations of the language are at your disposal as you’re just handling data) to (+ 1 1). It’s this operating on data between read and eval that macros do.
Re:A Year in Clojure by vnorilo on Sunday October 07, 2018 @10:34AM (#18160720)
Homoiconicity is the googlable term. The first Lisp book that blew my mind is SICP, nowadays free: https://sicp.me/sicp.pdf I think you need a big picture to appreciate the depth of homoiconicism and meta-circularity. It's a big one, but well worth it IMO.
Re:A Year in Clojure by kbp on Sunday October 07, 2018 @11:28AM (#18161016)
I remember finding it pretty revelatory the first time I saw LET defined as a macro like

    (defmacro let (bindings &body body)
      `((lambda ,(mapcar #'first bindings) ,@body)
        ,@(mapcar #'second bindings)))
Seeing it use MAPCAR like that just made something click for me for whatever reason.
Re:A Year in Clojure by disconcision on Sunday October 07, 2018 @02:11PM (#18161862)
To add on, Racket in particular has some very slick syntax for writing these kinds of things:

  (define-syntax-rule
    (let ([id expr] ...) body ...)
    ((λ (id ...) body ...) expr ...))
Re:A Year in Clojure by yomly on Sunday October 07, 2018 @07:41PM (#18163226)
I definitely liked this about Racket but I did also find the elipses to be a bit magic to begin with...
Re:A Year in Clojure by disconcision on Sunday October 07, 2018 @07:57PM (#18163285)
As with all things, the trick to chase the magic away is to implement them. I recently made a little term rewriting library using racket/match syntax but with the pattern/templates specifiable at runtime. Ellipses were a bit confusing to begin with but when you break list patterns down into cons pairs + a cons-like pattern binary combinator to represent ellipses, it started making sense. The unexpected thing for me was that I thought nested ellipses were going to be the tough part, but it turns out they actually just... work.
Re:A Year in Clojure by taylorwood on Sunday October 07, 2018 @11:28AM (#18161022)
Stuart Sierra has an interesting talk on homoiconicity and its history: https://www.youtube.com/watch?v=o7zyGMcav3c
Re:A Year in Clojure by jefffoster on Sunday October 07, 2018 @12:32PM (#18161368)
Paradigms of AI Programming by Peter Norvig is a beautiful illustration of code as data.

I learnt loads translating the Lisp to Clojure.

Re:A Year in Clojure by ced on Sunday October 07, 2018 @11:26AM (#18161004)
Re:A Year in Clojure by TheGrassyKnoll on Monday October 08, 2018 @01:19AM (#18164293)
Download for free. Thank you Mr. Graham.
Re:A Year in Clojure by 2_ghosts on Sunday October 07, 2018 @03:29PM (#18162219)
The concept of code as data became intuitive to me after reading "The Nature of Lisp".

https://www.defmacro.org/ramblings/lisp.html