• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Joy of Clojure - Questions

 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Authors,

Good to see the Book Promo in Clojure forum.

I had been looking at the clojure website and the wikipedia articles to get to know about Clojure and its features. But overall I am slightly confused.

1. What exactly is clojure? It is a functional programming language ?

2. Why should I need clojure? Where it will be of a good help, which other languages can't do?

3. It says Clojure is a dialect of Lisp. What extent the dialect is achieved?

4. I also read that Clojure is partially supporting OOP. I also read as it targets on JRE and CLR (.Net). I feel it is contradicting. As Java is a OOP and clojure targets to run on JRE, it should also be supporting it. Right? How the gap is addressed? What are the main design goals of having this partial supporting of OOP?

5. What is the significance of targeting both JRE and CLR?

6. The wikipedia article also says that clojure works on "code as data" principle. The term is fascinating but I could get very little of it. can you just brief about this?

Thanks in advance.
 
clojure forum advocate
Posts: 3479
Mac Objective C Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not the author but I will try to address a couple of points:
1. Clojure is Lisp dialect that runs on the JVM with great emphasis on concurrent programming.
Clojure is a functional language but not a pure one (please dear authors correct me if I'm wrong).
4. Clojure doesn't support OOP as we know it, it has its own ways to achieve a much powerful OOP paradigm like destructing, records, protocols and multi-methods.
If you want to build a language for the JRE you don't have to support OOP, Kawa is an implementation of Scheme language for example.
6. "Code as data" is crown jewel of the legendary freaky Lisp.
Code is created by the language own data structure, this will allows you to write programs that write programs (a.k.a knows as Meta-programming).
Macros allow you to create your very own language (Domain Specific Language).
To me Lisp is dream of every programmer, with Lisp you can create whatever you want.
 
author
Posts: 22
VI Editor Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whew, that's quite a stack of questions! If you don't mind, I'll
answer them a bit at a time instead of all once. In fact, examining
the verbosity of my answers below, I promise to be more succinct
next time.

1. Clojure is a functional programming language, but as we note in the
book that phrase doesn't have an impressively firm meaning, so a few
details are warranted. The fact that it supports functions as values
would have been sufficient at one time to earn it the label
"functional". These days that's not so unusual, so "functional"
often means that a language discourages the creation of functions that
change global state and instead encourages functions that do nothing
besides return a value based solely on the parameters passed in.
Clojure fits this definition as well, though it's not militant and
relies primarily on convention and social pressure to keep us unruly
programmers from creating impure functions.

Anyway, Clojure is new language in its own right, though it borrows
from many other languages to achieve its unique blend. In the mix are
LISP which primarily influences the syntax, ML which sways some of the
semantics, and other more strictly functional languages which
contribute to Clojure's powerful suite of high-order functions.

2. I think a person is unlikely to take on the daunting task of truly
understanding a new language unless they are at least somewhat
dissatisfied with the languages they already know. In my own
experience, when I've been happy with my current tool set, my attempts
to learn new languages have mostly caused me to become defensive and
rapidly lose interest. I could tell you a story about knowing Perl
and failing to learn Python over a tasty beverage sometime. So one
way to look at why you might want to learn Clojure is to list some of
the reasons that people become unhappy with their current language and
find joy in Clojure (see what I did there?). These also amount to
broad categories of Clojure's strengths which while not unique by
themselves form what I believe to be a compelling case when taken
together.

Metaprogramming: This is what brought me, a Python, Ruby, and C++
user, to even consider a LISP in the first place. I found myself
doing tortured things with the C preprocessor, C++ templates, Ruby's
method_missing, and Python metaclasses, all to support the creation of
simple, clean libraries. Compared to these, Clojure's macros are
stunningly simple and powerful.

Libraries: What made me go cold trying to use Common Lisp, and is
often the bane of interesting new languages, is the set of freely
available libraries for getting "real world" work done. Clojure
handily addresses this by simply co-opting every Java library in
existence, and making them often easier to use from Clojure than they
are from Java.

Concurrency: If you find writing multi-threaded programs to be tricky
and fraught with peril, Clojure will be a treat. It doesn't foist the
entire problem on the programmer ("here's a thread and lock, go knock
yourself out"). Nor does it attempt to provide a one-size-fits-all
silver bullet (I've seen STMs and actors models sometimes portrayed
this way). Instead, Clojure suggests a new way of thinking about
mutable state, data values, and object identities, and provides
a variety of tools that interact well with different requirements to
provide clean, simple solutions for most concurrency demands.

Infrastructure: I imagine members of this forum are particularly
aware of the rich world of tools available for deploying, analyzing,
debugging, and generally wrangling the JVM. Clojure fits neatly into
this world, embracing the JVM and allowing you to continue using most
of the tools you already know as you take advantage of Clojure's
benefits.

Immutables and functional programming: I mention these because in
retrospect they are key to why I enjoy using Clojure. But I didn't
know I wanted them before I learned Clojure, so going into detail here
not be very compelling. Instead I'll simply express my hope that you
consider learning some language that provides these, even if it
isn't Clojure, because knowing them will help you spot several common
kinds of bad code and help you to write better code in any language.

Well! That's enough for now! I'll take a crack at the rest of you
questions tomorrow, without spewing quite so many words.

--Chouser
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wow. Thank you so much for the patient and detailed explanations Chris. Much delighted.

Same as you. Just an instantaneous reply. Will read it fully and come with any further queries
 
Chris Houser
author
Posts: 22
VI Editor Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3. Clojure's basic syntax for function and macro calls is the same as any LISP. In fact the way macros are defined and operate in Clojure is almost identical to Common LISP. Also, the dynamic nature of the development process is quite LISP-like.

4. The contradiction you see is in Clojure running on the JVM but not fully supporting OOP? Well, there are two points to make here. One is that Clojure considers some features of OOP to be harmful. For example, providing a concrete base class without a set of interfaces that fully describes it is now generally discouraged even in Java -- Clojure takes this a step further by not supporting it at all in its preferred polymorphic forms (multimethods and protocols).

The point other is that sometimes these less than desirable features are required for interop with some existing Java code, and so most of them are supported via gen-class and/or proxy. There is a bit of a performance cost to using both of these, in exchange for getting access to more of these undesirable OOP features as well as more dynamic behavior that Java alone provides.

Perhaps, though, I didn't understand your question. If I missed your point, please feel free to rephrase it.

5. Clojure embraces its host platform, adding power and flexibility to a virtual machine runtime that already exists. This allows Clojure to co-exist with other languages on the same platform. I think it's not at all uncommon for Clojure to be added existing Java code bases. The CLR port allows you to take advantage of Clojure's features on that platform as well, and to incorporate Clojure in existing C# or other .Net code bases.

I'll take a crack at your final question tomorrow.
 
Chris Houser
author
Posts: 22
VI Editor Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raghavan Muthu wrote:6. The wikipedia article also says that clojure works on "code as data" principle. The term is fascinating but I could get very little of it. can you just brief about this?



"Code is data" is indeed fascinating, with broad but often subtle implications.

Several languages provide syntax for some kinds of literal data -- often array and hash tables, like JavaScript. So this is textual syntax that maps quite simply to in-memory data structures:

But they don't use this syntax for most of their code, even if parts of it look similar (such as curly braces):

Now undoubtedly inside the parser or compiler this is translated to an in-memory data structure, sometimes called an abstract syntax tree. But it's hard to guess exactly what shape it takes, and easy to imagine that it is clumsy to manipulate compared to the core array and hash-table data structures of the language.

So why not define a language's syntax to exactly match it's literal data
syntax? We could then perhaps rewrite the if statement above something like this:

Now it's quite clear what data structure will be constructed for that syntax, and we can easily imagine code that generates or manipulates structures like that, because our programs already create and use data like that when it's representing data.

This is "code is data". Of course the syntax for lists is a bit different, but Clojure has syntax for lists, hash maps, vectors, and sets, and this syntax is used both for its data and for its code. As with most LISPs, code is primarily made of up lists:

This is the key to macros being not just possible but convenient to write and use. It means it's easy to pass around not just objects and functions, but unevaluated code or code fragments. It means reading in, manipulating, and then saving code back out to files is fairly straightforward. It may not be immediately obvious how this helps, but I continue to be surprised by subtle ways my programs can benefit from the fact that code and data are described and manipulated exactly the same way.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[quote=Chris Houser]
Anyway, Clojure is new language in its own right, though it borrows
from many other languages to achieve its unique blend. In the mix are
LISP which primarily influences the syntax, ML which sways some of the
semantics, and other more strictly functional languages which
contribute to Clojure's powerful suite of high-order functions.
[/quote]

Well agreed. Thanks for the crisp intro.

[quote]
2. I think a person is unlikely to take on the daunting task of truly
understanding a new language unless they are at least somewhat
dissatisfied with the languages they already know. In my own experience, when I've been happy with my current tool set, my attempts
to learn new languages have mostly caused me to become defensive and rapidly lose interest.
[/quote]

well said!

[quote]
I could tell you a story about knowing Perl
and failing to learn Python over a tasty beverage sometime. So one
way to look at why you might want to learn Clojure is to list some of
the reasons that people become unhappy with their current language and
find joy in Clojure (see what I did there?).
[/quote]

Well, I am curious to know. Why don't you share the story?


[quote]
ython metaclasses, all to support the creation of
simple, clean libraries. Compared to these, Clojure's macros are
stunningly simple and powerful.
[/quote]

Cool. Can you please give a little more detail in this front?

[quote]
Clojure
handily addresses this by simply co-opting every Java library in
existence, and making them often easier to use from Clojure than they
are from Java.
[/quote]

This is what most of the new Java based language (Groovy etc.,) does. Right?

[quote]
Concurrency: If you find writing multi-threaded programs to be tricky
and fraught with peril, Clojure will be a treat. It doesn't foist the
entire problem on the programmer ("here's a thread and lock, go knock
yourself out").
[/quote]

Well, quite interesting! The interest increases towards Clojure!

Thank you very much for the detailed explanations once again Chris! :)
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chris Houser wrote:

This is "code is data". Of course the syntax for lists is a bit different, but Clojure has syntax for lists, hash maps, vectors, and sets, and this syntax is used both for its data and for its code. As with most LISPs, code is primarily made of up lists:

This is the key to macros being not just possible but convenient to write and use. It means it's easy to pass around not just objects and functions, but unevaluated code or code fragments. It means reading in, manipulating, and then saving code back out to files is fairly straightforward. It may not be immediately obvious how this helps, but I continue to be surprised by subtle ways my programs can benefit from the fact that code and data are described and manipulated exactly the same way.



Yes it explains. But i don't get the full meaning and purpose as you had described! May be I would get it when I too get the 'joy of closure'

Thanks Chris!
 
reply
    Bookmark Topic Watch Topic
  • New Topic