Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

I am primarily a Python, Java, C++ programmer, and I've gained a recent interest in functional language. I'm thinking of picking up a LISP dialect: Clojure. Now, I've been working through tutorials and whatnot, but I've noticed that beyond syntax differences, I don't feel that I am learning any fundamentally different way of solving problems.

So, are there any advantages of learning Clojure after Python in the way I can write programs?


migration rejected from May 17 at 17:54

This question came from our site for professional and enthusiast programmers. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.

closed as off-topic by Ixrec, durron597, gnat, GlenH7, Snowman May 17 at 17:54

This question appears to be off-topic. The users who voted to close gave these specific reasons:

  • "Questions seeking career or education advice are off topic on Programmers. They are only meaningful to the asker and do not generate lasting value for the broader programming community. Furthermore, in most cases, any answer is going to be a subjective opinion that may not take into account all the nuances of a (your) particular circumstance." – Ixrec, GlenH7, Snowman
  • "Questions about what language, technology, or project one should take up next are off topic on Programmers, as they can only attract subjective opinions for answers. There are too many individual factors behind the question to create answers that will have lasting value. You may be able to get help in The Whiteboard, our chat room." – durron597, gnat
If this question can be reworded to fit the rules in the help center, please edit the question.

I think this question is quite valid. All over SO people say "learn a functional language, it'll change the way you approach problems". So what, then, is wrong with asking how it is supposed to change your approach? Perhaps an edit to the question would be in order. What features of Clojure will help in understanding functional programming? –  Josh Smeaton Mar 14 '11 at 2:41
I think the problem is that there's already many variants of this question for Lisp ("I've tried Lisps, but I don't get it", "Is Learning LISP useful?", etc.). Just how many "Why Lisp?" questions do we need? –  Ken Mar 14 '11 at 5:46
Have not you noticed macros? It is a fundamentally new concept for a Python programmer, you can't have anything comparable in neither of the languages you're using (even C++ templates are too far from that). But, it have nothing to do with a functional programming too. –  SK-logic Mar 14 '11 at 10:02

2 Answers 2

James Gosling put it very well in in talking about optimization.

So what is Newton's Method? It's kind of like a function that takes two functions as parameters (f(x) and f'(x)). But the second parameter is should really be derived from the first. f'(x) can be approximated by a little numerical trickery, but it's a numerically dicey thing to do. One of the big drawbacks with closures and function pointers is that you just get to execute the function. You can't do any introspection. This is one of those times where I miss Lisp macros: the ability to rip open a function and do wild things like symbolic differentiation was wonderful. My PhD thesis project was filled with such hackery.

The problem is that you need both the function and the first derivative. In most languages you can only execute the code, but in Lisp you can treat the code as data, and find out what it does, and do the symbolic differentiation yourself.

See the difference?

+1 just because it demonstrates an interesting reason to look at Lisp and not just at functional programming in general. –  CodexArcanum Mar 14 '11 at 19:44
Does anyone have more information on what Gosling is talking about here? I've got a basic understanding of Common Lisp style macros from books like Practical Common Lisp and Land of Lisp, but I can't imagine this: "rip open a function and do wild things like symbolic differentiation was wonderful". That sounds like using some kind of introspection to get the source of the body of a function; is that possible in any Lisp? Alternatively, does anyone know where I could get a copy of Gosling's thesis? I couldn't find anything when I searched... –  spacemanaki Mar 15 '11 at 15:00
@spacemanaki, to my understanding you can ask the Lisp system for the token tree corresponding to a given function. You can then analyze this tree as you would any other tree - like the DOM tree in XML - and symbolic differentiation is not that difficult. A *-node becomes a +-node with each argument recursively differentiated too, etc. –  user1249 Mar 19 '11 at 22:56
@Thorbjørn Ravn Andersen Can you provide some citation for that? Which Lisp systems, Scheme or Common Lisp? I understand how it would work, but what I was saying was that I don't think that's true for general functions. Someone in #lisp claimed that it's not possible, and that makes sense to me. Some Scheme/Lisp systems are compiled, and by the time the function object evaluated the source is not available. The only way to do this, and possibly how Gosling did in his dissertation, would be to wrap every function definition in a macro. That being said, I don't know for sure. –  spacemanaki Mar 20 '11 at 1:12
Over a month later, someone asked about this on SO, and the answer turned up function-lambda-expression which does exactly what Gosling described, returning the source code of the function. Implementations are not required by the spec to support this though (SBCL doesn't seem to, but CLisp does). You could definitely implement a symbolic differentiation package this way. –  spacemanaki May 1 '11 at 20:35

Some ideas:

  • Master concurrency using Clojure's persistent data structures and STM-managed references. See this video by Rich Hickey. In my view, this is possibly the most exciting feature of Clojure as it offers a credible route to building highly concurrent applications for multi-core machines that are free from locking issues.

  • Exploit the fact that Clojure is a homoiconic language (as are all Lisps) and you can therefore treat code-as-data. To see the power of this, try writing some macros that generate Clojure code at compile-time..... you can use this to generate your own syntax and language constructs which is much more powerful than most other languages. Try implementing a proper short-circuiting "and" operation as a function call, for example - in most languages you can't because the arguments will both get evaluated before the call to "and". But with macros this is easy.

  • Write some non-trivial code in a functional programming style. Coming from any imperative or OOP background this should be a good experience. e.g. use Ring to write a web server as a single function..... see this great video for inspiration.

  • Read SICP and do the exercises in Clojure (it's not particularly hard and a good mental exercise to translate the examples from Scheme to Clojure). This book is a masterpiece of MIT computer science education and will certainly broaden your mind if you haven't explored these kinds of concepts before. There is also a "SICP in Clojure" project that you can google although I'm not sure how complete that is so far.

  • Have fun with lazy evaluation. Write some algorithms using infinitely large data structures. Project Euler has quite a few challenges that are quite amenable to this kind of approach (e.g. creating a lazy list of all prime numbers.....)

+1, also for stressing lazy evaluation and infinite data structures (streams). I have been exploring this lately and once you have understood the ideas you can use them in other languages too. I have first explored streams in Scheme and Scala and now I am rewriting some ugly Java code based on iterators to much cleaner code using immutable streams and lists (using lazy evaluation and memoization). I think this concept should be used more often because it is really elegant and powerful. –  Giorgio Apr 8 '13 at 9:19
So, I think learning streams in a more functional language like Scheme (or Clojure, Haskell, Scala, etc) can really help to better understand this concept and make it easier to use it in a non-functional language like Python. –  Giorgio Apr 8 '13 at 9:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.