concept program in category clojure

appears as: program, program, programs, The program, programs
Get Programming with Clojure

This is an excerpt from Manning's book Get Programming with Clojure.

In Clojure, the natural way to manipulate data is the immutable way which means that data cannot be changed by the program.

Immutability is highly valuable because it is much easier for a programmer to understand and debug a program where it is guaranteed that the data is not modified. Many bugs that use to happen frequently in other programming languages are just not happening in Clojure because of the immutability.

When we run a Clojure program from the command line, we specify the namespace where the entry point of the program is located. Currently, we will use namespaces mechanically. Namespaces will be covered in details in 11. The main thing to know about namespaces is that there is a one-to-one correspondence between namespaces and source files. Clojure files have the .clj suffix. For instance, the namespace corresponding to a file named hello.clj is hello.

Now, if the entry point of our program is in a namespace named hello, we run the program with this command line:

Create a program that receives a name and prints it twice to the console with a comma between the two names.

Ex 3:

  

Create a program that receives a name and a number and:

Clojure in Action, Second Edition

This is an excerpt from Manning's book Clojure in Action, Second Edition.

The promise of Clojure’s synthesis of features is a language that’s composed of simple, comprehensible parts that not only provide power and flexibility to writing programs but also liberate your understanding of how the parts of a language can fit together. Let no one deceive you: there are many things to learn. Developing in Clojure requires learning how to read and write Lisp, a willingness to embrace a functional style of programming, and a basic understanding of the JVM and its runtime libraries. We’ll introduce all three of these Clojure pillars in this chapter to arm you for what lies ahead in the rest of the book: a deep dive into an incredible language that’s both new and old.

The preceding quote is from a presentation by Rich Hickey in which he discusses Clojure’s approach to concurrency and state. He means that most languages use an approach to modeling state that doesn’t work very well. To be precise, it used to work when computers were less powerful and ran programs in a single-threaded fashion, but in today’s world of increasingly multicore and multi-CPU computers, the model has broken down. This is evidenced by the difficulty of writing bug-free multithreaded code in typical object-oriented (OO) languages like Java and C++. Still, programmers continue to make the attempt, because today’s demands on software require that it take advantage of all available CPU cores. As software requirements grow in complexity, parallelism is becoming an implicit requirement.

As you’ve seen, objects (such as x or favorite-movies) don’t have to physically change for programs to handle the fact that something has happened to them. As discussed previously, they can be modeled as references that point to different objects over time. This is the flaw that most OO languages suffer from: they conflate identities (x or favorite-movies) and their values. Most such languages make no distinction between an identity such as favorite-movies and the memory location where the data relating to that identity is stored. A variable kyle, for example, might directly point to the memory location containing the data for an instance of the Person class.

In typical OO languages, when a destructive method (or procedure) executes, it directly alters the contents of the memory where the instance is stored. Note that this doesn’t happen when the same language deals with primitives, such as numbers or strings. The reason no one seems to notice this difference in behavior is that most languages have conditioned programmers to think that composite objects are different from primitives such as strings and numbers. But this isn’t how things need to be, and there is another way. Instead of letting programs have direct access to memory locations via pointers such as favorite-movies and allowing them to change the content of that memory location, programs should have only a special reference to immutable objects. The only thing they should be allowed to change is this special reference itself, by making it point to a completely different, suitably constructed object that’s also immutable. This concept is illustrated in figure 6.2.

The Joy of Clojure, Second Edition

This is an excerpt from Manning's book The Joy of Clojure, Second Edition.

But the textual characters work to easily demarcate lists to the reader.[6] Lisps (and Clojure is no exception) have chosen to mirror the list-like textual syntax with the list data structure.[7] By mirroring the syntax with the underlying data representation of code, there’s little conceptual distance between the form of the code on the screen and the code that’s fed into the compiler. This short conceptual gap facilitates the manipulation of the data that represents a program using Clojure.

If you’re unaccustomed to writing logic programs with systems like core.logic, this chapter has likely exposed you to a different way of thinking about programs. We’ve found that Clojure has motivated us to think differently about building programs via its internal “why” and “how” and libraries like core.logic. In the next and final chapter of the book, we’ll share some of the ways Clojure has helped us look at problems and their solutions in new ways.