1

I am a beginner Clojure programmer. In a Book, it is an advantage of the Clojure that the code that is not evaluated is data. But I do not understand. So, I want an example code and an explanation so I can understand.

I am Korean. For that reason, I would appreciate it if you could understand even if I wrote an awkward sentence or failed to keep my manners.

Necto
  • 2,594
  • 1
  • 20
  • 45
  • 1
    This is an excellent question, though not typically on topic for this forum. Please don't be discouraged if it gets closed, but here people are looking for more concrete questions about problems with specific code. If you search the web you will find a lot of commentary on this topic and the more you read the more you will understand. Also the more you program in clojure or similar languages you will understand more. As a beginner you may not feel it, but as you progress, espeically if you start writing macros, you surely will!. – jas Jun 07 '20 at 12:01
  • 1
    Some places to start: https://en.wikipedia.org/wiki/Homoiconicity https://blogs.mulesoft.com/dev/news-dev/code-is-data-data-is-code/ – jas Jun 07 '20 at 12:01

2 Answers2

6

I think the question you are asking is: why is it an advantage for a programming language that its source code is available within the language as data (and in particular as structured data, not just as strings)?

The reason it is an advantage is simple: once you have access to a data structure representing the source of a program you can manipulate that structure: you can write programs that manipulate other programs.

A particularly good case is where the surface form of the language and the data structure which represents it is rather 'low-commitment': it doesn't encode much meaning of the program, just its syntactic form.

Then you can write programs in this low-commitment language which include constructs which don't yet exist in the language. And you write other programs which take these programs and turn them into other programs which only use constructs which already exist in the language.

And you can keep doing this. So you can start with a language which is whatever language you are given, and incrementally build it into a language which is the language you want.

Of course you can do this with almost any language: in almost any language that lets you read files into strings, parse those strings into some representation of a language, with extensions, and then process that representation into the original language which you then hand off to the compiler or interpreter.

But Lisp-family languages make this much easier for you:

  • they do the parsing-into-a-data-structure for you, so 'source code is data';
  • they have an intentionally low-commitment source form, leaving it up to you what given constructs mean;
  • they provide facilities to let you do this processing-of-source-code-into-other-source-code in a fairly painless way, by defining macros.

That's the advantage of having source code available as structured data in the language: it's a crucial part of the things you need to make implementing your own programming language (which is often really a programming jargon – a language which inherits all of a base language but adds something of its own on top of it) easy.

1

I think it is in "Mastering Clojure Macros" that I saw an example of "code is data" that made a lot of sense (at least to me).

Syntactically speaking this is valid Clojure code: it's a simple list of numbers and symbol.

(1 + 1)

But it is not a valid program! If you evaluate this in your REPL it will throw an error because 1 isn't a function.

When Clojure reads this text, it produces a list (the same list) and allows things such as a macro to receive it (unevaluated), transform it and send it back. Perhaps that macro could simply swap the first two elements and return (+ 1 1)?

This would not have been possible in JavaScript for example:

var a = + 1 1; // Syntax error

The engine would have exploded way before you could try anything!

customcommander
  • 17,580
  • 5
  • 58
  • 84