views:

1274

answers:

18

My professor told us that we could choose a programming language for our next programming assignment. I've been meaning to try out a functional language, so I figured I'd try out clojure. The problem is that I understand the syntax and understand the basic concepts, but I'm having problems getting everything to "click" in my head. Does anyone have any advice? Or am I maybe picking the wrong language to start functional programming with?

+6  A: 

You could check out The Little Schemer.

Andrew Hare
+13  A: 

It's a little like riding a bike, it just takes practice. Try solving some problems with it, maybe ProjectEuler and eventually it'll click.

Someone mentioned the book "The Little Schemer" and this is a pretty good read. Although it targets Scheme the actual problems will be worth working through.

Good luck!

Sean
+1  A: 

I'd start with a language that can be interpreted. I found Moscow ML to be fairly easy. It is a lightweight implementation of Standard ML.

boutta
The strong typing might be a hindrance. If it doesn't bother you, awesome! Fabulous language, IMHO.
sam
I found it quite a help while learning programming, since you need to reflect on what the program is going to do and what type will be used to achieve it. And as a side effect you learn to programm type safe. Quite useful now with Java.
boutta
"I'd start with a language that can be interpreted" -- you mean, like Clojure?
Aaron
Exactly! But I don't know why one should roll out a complete JVM only to use a functional programming language. But I suppose this is personal preference.
boutta
+1  A: 

My personal practice is to find a small project (something that might take 3-5 nights hacking away) and implement it. How about a blog filter tool? Maybe just a Towers of Hanoi or Linked List implementation (those are usually 1-night projects).

The way it usually works out is I implement it poorly the first time, throw away what I had, and it finally clicks a few hours in.

A HUGE help is taking a course in something like... um... LISP! :) The homework will force you to confront a lot of the concepts and it clicked for me long before the semester ended.

Good luck!!

sam
+4  A: 

For educational purposes I would recommend PLT Scheme. It is a portable and powerful environment with very good examples and an even better documentation. It will help you to discover the thoughts behind functional programming step by step and in a very clean way. Choosing a little application to implement will help you learning the new language.

http://www.plt-scheme.org/

Additionally "Structure and Interpretation of Computer Programs" of H. Abelssn, G. Sussman, and J. Sussman is a very good book regarding Scheme (and programming).

Regards

mue

Mue
+2  A: 

Just remember: it's all data!

L. De Leo
+1  A: 

Good luck. It took me until about halfway through the "Programming Languages" course in college before Scheme "clicked". Once that happened, though, everything just made sense, and I fell in love with functional programming.

Write a Lisp interpreter in Lisp.

Adam Jaskiewicz
+1  A: 

If you haven't alrady, read up on what makes lisp a unique language. If you don't do this first, you'll be trying to do the same things you could do in some other programming languages.

Then try to implement some small thing (try to make it useful to you or you might not have the motivation).

Lisp in a box is a great way to get your feet wet.

temp2290
+3  A: 

Some thoughts on Lisps, not specific to Clojure (I'm not a Lisp expert, so I hope they're mostly correct and useful):

Coding in AST

I know little about compiler or interpreter theory, but every time I code in Lisp, it amazes me that it feels like directly building an AST.

That's part of what "code = data" means, coding in Lisp is a lot like filling data structures (nested lists) with AST nodes. Amazing, and it's easy to read too (with the right text editor).

A Programmable Programming Language

So code chunks are just nested lists, and list operations are part of the language. So you can very easily write Lisp code that generates Lisp code (see Lisp macros). This makes Lisp a programmable (in itself!) programming language.

This makes building a DSL or an interpreter in Lisp is very easy (see also meta-circular evaluation).

Never reboot anything

And in most Lisp systems, code (including documentation) can be introspected and hot swapped at run time.

Advanced OOP

Then, most Lisp Systems have some sort of Object System derived from CLOS, which is an advanced (compared to many OOP implementations) and configurable Object System (see The Art of the Metaobject Protocol).

All these features where invented long ago, but I'm not sure they are available in many other programming languages (although most are catching up, e.g. with closures), so you have to "rediscover" and get used to these by practicing (see the books in other answers).

Sébastien RoccaSerra
+10  A: 

Well, for me, I encountered the same problem as you do in the beginning when I started doing OCAML, but the trick is that you must start thinking about what you want from the code and not how to do it!!!

For example, to calculate the square of list's elements, forget about the length of the list and such tricks, just think mathematically like that:

  • if the list is empty -> I am done

  • if not, then the list must have a head and tail -> you calculate the square of the head, then ask your function to do the same with the tail.

Just think about the general case and the base one, and that you are emitting data and not modifying it (unless you want to modify it ;) ).

Good luck!

martani_net
+1  A: 

Who said it is going to click? I'm always confused

But if you think about how much abstraction it is possible to hide away, behind lisp macros. Then your brain will explode.

:)

Flinkman
+1  A: 

For me the important thing is to make sure you do everything in a 'lisp-y' way. Don't be tempted to think 'In Java I'd use a for loop here, how do I do for loops in Lisp?' but to go through enough examples and tutorials (as someone pointed out, SICP is perfect for this) that you can start to spot when code looks 'Lisp-y' and recognise common language paradigms.

I certainly know the feeling of looking at some code I've just written and intuitively knowing that it's correctly idiomatic for that language and platform/framework - that, I think, is when it 'clicks'.

Edit: And kudos for choosing a functional language, lesser students would have just done it in Java :)

Mark Pim
+2  A: 

Write some simple classic functions that Lisp is good at, like

  • reverse a list

  • tell if an atom is somewhere in an s-expression

  • write EQUAL to tell if 2 s-expressions are equal

  • write FRINGE to get the list of atoms at the fringe of an s-expression

  • write SUBST, then write SUBLIS

  • Symbolic differentiation

  • Algebraic simplification

  • write a simple EVAL and/or APPLY

Understand that Lisp is good for these kinds of no-side-effect functional programs.

It is also useful for stateful side-effect (non-functional) programs, but those are more like "programs" than "functions".

Which is better for a given application depends on the application. In general, it should contain no less, and no more, state information than necessary.

Mike Dunlavey
+1  A: 

I'd check out Programming Clojure. It's a great book for non-lispers.

Rayne
+1  A: 

In addition to what other SO'ers have already suggested, here are my 2 cents:

  1. Start learning the language and try out a few simple numerical/hobby problems in the language
  2. IMPORTANT: Post the solution/code to StackOverflow, asking for people's opinion if that is really the LISPy way to do it.

Best of luck!

Amit
+1  A: 

Easy!

M-x  lisp-mode

OK, OK, so you might not have Emacs for a brain. In all seriousness, what you need to do is to get really good at recursion. This can be quite a brain warp initially when trying to extend the concept of recursion beyond the canonical examples, but ultimately it will result in more fluid, lispy code.

Also, a lot of people get hung up on the parenthesis, and I don't really know why - the syntax is very simple and consistent and can be mastered in minutes. For me, I came to Scheme after having learned C++ and Java, and I always thought that the difference between "functions" and "operators" was a false dichotomy, and it was refreshing to see that distinction eliminated.

As far as functional programming goes, as long as you can wrap your head around the fact that a function is a first-class value and can be passed both into and out of other functions you should be fine. The usefulness of this will become clear over time, but it's enough that you can write function-taking and function-returning functions.

Finally, I'm not sure what support Clojure has for macros, but they're considered an essential part of lisp. However, I wouldn't worry about learning them until you're deeply familiar with the above items - though macros are incredibly useful and versatile, they're also used less often than the other techniques I mentioned.

Kyle Cronin
+3  A: 

Take a look at 99 Lispy problems

Abhijith
+2  A: 

How about this: http://www.defmacro.org/ramblings/lisp.html

It is a very simple, step-by-step introduction to thinking in lisp from the point of view of a regular imperative programmer (Java, C#, etc.).

Joshua Hayworth