Simple functional programming lexicon?

45 views
Skip to first unread message

Ben Armstrong

unread,
Mar 17, 2010, 8:28:21 AM3/17/10
to Clojure
I am new to clojure and functional programming, but not to programming
in general, having been in the profession for a quarter of a century.
I'm trying to stretch myself a bit by learning clojure, but some of the
threads here go beyond mere stretching to verge on head-exploding. In
my offline reading of this list this morning, I tried using the tools
available to me at the time (dict client and a few dictionaries like
foldoc) to make sense of words like: http://foldoc.org/monad . I
survived to write this post, but only barely.

What I would like to have is some sort of lexicon to at least help
explain the terminology in a way that doesn't require three years of
academic exposure to functional programming to read. Is there such a
reference? Or should I just ignore threads like "Why do functions in
the state monad only accept one value?" and be happy that at least
somebody cares about this stuff, so that I don't have to?

Ben

Sean Devlin

unread,
Mar 17, 2010, 2:58:53 PM3/17/10
to Clojure
Here's a collection of reading material:

http://news.ycombinator.com/item?id=1033503

Personally, I'd recommend Programming Clojure as a good place to
start.

Sean

On Mar 17, 8:28 am, Ben Armstrong <synerg...@gmail.com> wrote:
> I am new to clojure and functional programming, but not to programming
> in general, having been in the profession for a quarter of a century.  
> I'm trying to stretch myself a bit by learning clojure, but some of the
> threads here go beyond mere stretching to verge on head-exploding.  In
> my offline reading of this list this morning, I tried using the tools
> available to me at the time (dict client and a few dictionaries like

> foldoc) to make sense of words like:  http://foldoc.org/monad.  I

David Nolen

unread,
Mar 17, 2010, 3:54:25 PM3/17/10
to clo...@googlegroups.com
On Wed, Mar 17, 2010 at 8:28 AM, Ben Armstrong <syne...@gmail.com> wrote:
What I would like to have is some sort of lexicon to at least help explain the terminology in a way that doesn't require three years of academic exposure to functional programming to read.  Is there such a reference?  Or should I just ignore threads like "Why do functions in the state monad only accept one value?" and be happy that at least somebody cares about this stuff, so that I don't have to?

Ben

To me the beauty of Clojure isn't that you _don't_ need a strong academic background or even much exposure to FP to approach the language. Clojure is relatively free of FP jargon, instead we have "Rich Hickey Jargon". I jest :)

But seriously, in my personal opinion Monads are relatively useless in the context of Clojure. They are necessary in pure FP languages like Haskell because otherwise it's impossible get any real applications written. This is not to say they are not useful, you just don't need to know a damn thing about them to have fun in Clojure.

Having hacked on Clojure for a a year and half now I think the best approach is to learn those aspects of Clojure that are most useful to you. You certainly don't need to pick up everything at once. I still don't completely grok deftype/protocols or the more involved concurrency constructs. This hasn't stopped me from writing many useful programs.

If you _do_ want to get deeper in Clojure's philosophy you can't beat Rich Hickey's Amazon Bookshelf.

David


James Reeves

unread,
Mar 17, 2010, 4:02:24 PM3/17/10
to clo...@googlegroups.com
On 17 March 2010 19:54, David Nolen <dnolen...@gmail.com> wrote:
> But seriously, in my personal opinion Monads are relatively useless in the
> context of Clojure.

I'm considering using the continuation monad to implement a parser
that can pick up where it left off. However, I agree that they're very
rarely used in Clojure.

- James

Konrad Hinsen

unread,
Mar 17, 2010, 4:09:32 PM3/17/10
to clo...@googlegroups.com
On 17 Mar 2010, at 20:54, David Nolen wrote:

> But seriously, in my personal opinion Monads are relatively useless
> in the context of Clojure.

I'd say that in any impure functional language, monads are useful in
the context of specific applications or algorithmic approaches. When
working with continuations, or when writing complex parsers, monads
are useful. It's just Haskell that has a language-specific relation to
monads, because it needs them for fundamental tasks such as I/O.

Konrad.


David Nolen

unread,
Mar 17, 2010, 4:13:15 PM3/17/10
to clo...@googlegroups.com

James, and Konrad sorry if I sounded overly dismissive of Monads ;) Yes for parsers, continuations, other tricky problems they provide a very elegant solution.

David

eyeris

unread,
Mar 17, 2010, 4:57:04 PM3/17/10
to Clojure
On Mar 17, 7:28 am, Ben Armstrong <synerg...@gmail.com> wrote:
> Or should I just ignore threads like "Why do functions in the state monad only accept one value?"

Yes, ignore those for now. Write an entire program in Clojure before
you read anything about monads. They aren't required. When I first
dipped my toes in the functional waters I learned a lot from "Casting
SPELs in LISP"[1]. It's based around Common Lisp, but it should be
easily translatable to Clojure.

[1] http://www.lisperati.com/casting.html

Ben Armstrong

unread,
Mar 17, 2010, 7:01:38 PM3/17/10
to clo...@googlegroups.com
On 17/03/10 03:58 PM, Sean Devlin wrote:
> Here's a collection of reading material:
>
> http://news.ycombinator.com/item?id=1033503
>
> Personally, I'd recommend Programming Clojure as a good place to
> start.
>

Thanks. Yes, I have already started with "Programming Clojure". It is
because it doesn't go deeply into FP ('monad' is mentioned once, in an
aside, and is unexplained in that reference) that I was looking for
other references. I'll have a look at some of the others on this page.

Ben

jonathan...@gmail.com

unread,
Mar 17, 2010, 8:02:12 PM3/17/10
to Clojure
On Mar 17, 7:28 am, Ben Armstrong <synerg...@gmail.com> wrote:
> I am new to clojure and functional programming, but not to programming
> in general, having been in the profession for a quarter of a century.  
> I'm trying to stretch myself a bit by learning clojure, but some of the
> threads here go beyond mere stretching to verge on head-exploding.  In
> my offline reading of this list this morning, I tried using the tools
> available to me at the time (dict client and a few dictionaries like
> foldoc) to make sense of words like:  http://foldoc.org/monad.  I

> survived to write this post, but only barely.
>
> What I would like to have is some sort of lexicon to at least help
> explain the terminology in a way that doesn't require three years of
> academic exposure to functional programming to read.  Is there such a
> reference?  Or should I just ignore threads like "Why do functions in
> the state monad only accept one value?" and be happy that at least
> somebody cares about this stuff, so that I don't have to?
>
> Ben


Ben,
You can do a LOT with just a basic level of Clojure. I'm writing a
database
loader to help support an iPhone app I'm writing, and got done in just
a few
hours whilst only scratching the surface of what's available. No
paralleism,
no monads, no macros, no refs.
My opinion, and it may only be valid for me, is learning a language
works
best when you have a definite task, and you can build code, then
better
code, then even better code in the language you're trying to learn.
I have a couple of the books, but as yet, the best method for
learning
seems to be google and example code.
Tim Bray has a Clojure Wide Finder series that makes interesting
reading.
The best thing about Clojure is the immediacy and ability to syntax
check
as you go. I'm using TextMate with the Clojure bundle and it's working
great,
just hit clover-R on hightlighted text and the result comes right
back...
In short don't worry about monads... The terminology is awful, and
they make
my head explode too.
Here's a nice little piece of code that sucks in a file and inserts it
into a SQL
table. This is the sort of conciseness I love about Clojure.

(defn load-records [table-name file-name]
(doseq [line (-> file-name java.io.FileReader.
java.io.BufferedReader. line-seq)]
(let [fields (map #(.replace %1 "~" "") (seq (.split #"\^" (str line
"~"))))]
(sql/insert-rows table-name fields))))

If this sucks as code, someone jump in and tell!

Best Wishes,
Jonathan

Ben Armstrong

unread,
Mar 17, 2010, 8:16:31 PM3/17/10
to clo...@googlegroups.com
On 17/03/10 04:54 PM, David Nolen wrote:
> To me the beauty of Clojure isn't that you _don't_ need a strong
> academic background or even much exposure to FP to approach the
> language. Clojure is relatively free of FP jargon, instead we have
> "Rich Hickey Jargon". I jest :)

Very good. And so far, my experience has borne this out. It is
approachable, elegant, immediately usable. It's just that I am curious,
so I would like to be able to have at least a basic idea of what these
other conversations are about, even if I can't entirely follow every
argument.

> But seriously, in my personal opinion Monads are relatively useless in
> the context of Clojure. They are necessary in pure FP languages like
> Haskell because otherwise it's impossible get any real applications
> written. This is not to say they are not useful, you just don't need
> to know a damn thing about them to have fun in Clojure.

OK. That puts it a bit more in perspective. Also, once I was online
again, I reached for Wikipedia to see if I could find an article written
to be understood by those outside the field. Actually, the first two
paragraphs here were helpful to get at least a basic grasp of what
Monads are about:

http://en.wikipedia.org/wiki/Monad_(functional_programming)

The overview here is certainly more readable than the foldoc entry I
read, and it, in turn links to a page on monad as used in category
theory which is, I am sure, readable by mathematicians, but which again
left me scratching my head just as the foldoc entry had. So I guess
it's not so much FP that I find hard to grok as it is the math from
which it has borrowed some terminology.

> Having hacked on Clojure for a a year and half now I think the best
> approach is to learn those aspects of Clojure that are most useful to
> you. You certainly don't need to pick up everything at once. I still
> don't completely grok deftype/protocols or the more involved
> concurrency constructs. This hasn't stopped me from writing many
> useful programs.

Most useful to me, or at least most interesting ... I can't say that at
this point I can see where I'm going to use this. But it certainly *is*
intriguing ... including the bits that right now are a hard to follow at
least in part due to the unfamiliar terms.

> If you _do_ want to get deeper in Clojure's philosophy you can't beat
> Rich Hickey's Amazon Bookshelf.

I'm not sure at this point how deep I want to go. Nice resource, though.

Ben


Ben Armstrong

unread,
Mar 17, 2010, 8:23:05 PM3/17/10
to clo...@googlegroups.com
On 17/03/10 05:57 PM, eyeris wrote:
> On Mar 17, 7:28 am, Ben Armstrong<synerg...@gmail.com> wrote:
>
>> Or should I just ignore threads like "Why do functions in the state monad only accept one value?"
>>
> Yes, ignore those for now. Write an entire program in Clojure before
> you read anything about monads. They aren't required.When I first

> dipped my toes in the functional waters I learned a lot from "Casting
> SPELs in LISP"[1]. It's based around Common Lisp, but it should be
> easily translatable to Clojure.
>
> [1] http://www.lisperati.com/casting.html
>

Nice! I see there's a Ruby adaptation too, and *that*, at least, is
familiar ground.

Cheers,
Ben

Praki Prakash

unread,
Mar 17, 2010, 11:50:09 PM3/17/10
to clo...@googlegroups.com
I spent a lot of time trying to understand monads in Haskell, which led to reading Category Theory. It's an alluring subject and one can spend a lot time following the concepts and terminology. But, the good news is that, one doesn't need an advanced degree in math to appreciate monads in FP. A really helpful paper that explains the motivation for Monads is this: http://homepages.inf.ed.ac.uk/wadler/papers/marktoberdorf/baastad.pdf

As others have mentioned, Haskell is heavy in its use of monads and  many other  algebraic structures. I don't know why the two languages feel so different with respect to the level of formalism you need to use them. But, Haskell is one of the most mind bending programming language there and well worth the study.

Praki



--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en



--
http://www.google.com/profiles/praki.prakash

Konrad Hinsen

unread,
Mar 18, 2010, 3:14:52 AM3/18/10
to clo...@googlegroups.com
On 18.03.2010, at 04:50, Praki Prakash wrote:

> As others have mentioned, Haskell is heavy in its use of monads and many other algebraic structures. I don't know why the two languages feel so different with respect to the level of formalism you need to use them. But, Haskell is one of the most mind bending programming language there and well worth the study.

The difference is perhaps more in the community than in the language. But Haskell clearly shows its academic origins, whereas Clojure clearly shows the pragmatic attitude of its creator. I can appreciate both, being in the academic world but not in computer science - programming for me is mostly pragmatic rather than a research goal in itself.

Konrad.

LauJensen

unread,
Mar 18, 2010, 4:06:30 AM3/18/10
to Clojure
Hi Ben,

I think we often get the impression that functional programming is
directly connected to monads, but in practical terms the important
concepts are pure functions and persistent immutable datastructures.
The learning curve when coming from an imperative language, lies (for
me at least) mostly in avoiding looping/mutation and thinking in terms
of purely functional constructs and sequences. The surprising thing
is, that 99% of your code then turns out to be immutable and where you
would normally introduce state, you now don't have to. There are of
course things like I/O which are inherently stateful.

So long story short, read anyone of the many interesting blog posts
floating around on how to tackle real world problems using idiomatic
Clojure and you should be able to quickly grasp the jargon and
terminology. In my opinion you can entirely dismiss monads for the
first year or so of your Clojure experience.

If I'm over simplifying, feel free to correct me :)

Lau

On 17 Mar., 13:28, Ben Armstrong <synerg...@gmail.com> wrote:
> I am new to clojure and functional programming, but not to programming
> in general, having been in the profession for a quarter of a century.  
> I'm trying to stretch myself a bit by learning clojure, but some of the
> threads here go beyond mere stretching to verge on head-exploding.  In
> my offline reading of this list this morning, I tried using the tools
> available to me at the time (dict client and a few dictionaries like

> foldoc) to make sense of words like:  http://foldoc.org/monad.  I

Michael Kohl

unread,
Mar 18, 2010, 5:57:50 AM3/18/10
to clo...@googlegroups.com
On Wed, Mar 17, 2010 at 1:28 PM, Ben Armstrong <syne...@gmail.com> wrote:
> What I would like to have is some sort of lexicon to at least help explain
> the terminology in a way that doesn't require three years of academic
> exposure to functional programming to read.  Is there such a reference?  Or
> should I just ignore threads like "Why do functions in the state monad only
> accept one value?" and be happy that at least somebody cares about this
> stuff, so that I don't have to?

There's a really nice article series on monads in Clojure:

http://onclojure.com/2009/03/05/a-monad-tutorial-for-clojure-programmers-part-1/

Also the "Learn you a Haskell for Great Good" tutorial is a pretty
nice and light-hearted introduction to FP with Haskell which might
also help you to understand some of the concepts better:

http://learnyouahaskell.com/

Michael

Ben Armstrong

unread,
Mar 18, 2010, 3:56:47 PM3/18/10
to clo...@googlegroups.com
On 18/03/10 06:57 AM, Michael Kohl wrote:
> There's a really nice article series on monads in Clojure:
>
> http://onclojure.com/2009/03/05/a-monad-tutorial-for-clojure-programmers-part-1/
>

Oh, wow! Lucidly written. And it gives me something, maybe-m, that I
can't believe I got along without before. I'm eager now to continue
with part 2 ...

> Also the "Learn you a Haskell for Great Good" tutorial is a pretty
> nice and light-hearted introduction to FP with Haskell which might
> also help you to understand some of the concepts better:
>
> http://learnyouahaskell.com/
>

I've filed this one away. I had better stick with clojure for now until
I get more comfortable with it. Thanks. :)

Ben

Michael A Wright

unread,
Mar 17, 2010, 4:10:20 PM3/17/10
to Clojure

Michael Richter

unread,
Mar 18, 2010, 6:11:12 AM3/18/10
to clo...@googlegroups.com
Also the "Learn you a Haskell for Great Good" tutorial is a pretty
nice and light-hearted introduction to FP with Haskell which might
also help you to understand some of the concepts better:

http://learnyouahaskell.com/


If you don't mind taking a detour into the Haskell world, the book Real World Haskell also does a very good job of explaining key functional vocabulary using pragmatic examples (for the most part).  I may never use Haskell in anger, but I found learning the language and reading this one book a very good exploration of the functional programming space. 

--
"Perhaps people don't believe this, but throughout all of the discussions of entering China our focus has really been what's best for the Chinese people. It's not been about our revenue or profit or whatnot."
--Sergey Brin, demonstrating the emptiness of the "don't be evil" mantra.

Michał Marczyk

unread,
Mar 19, 2010, 9:52:51 PM3/19/10
to clo...@googlegroups.com
On 18 March 2010 20:56, Ben Armstrong <syne...@gmail.com> wrote:
> On 18/03/10 06:57 AM, Michael Kohl wrote:
>> There's a really nice article series on monads in Clojure:
>> http://onclojure.com/2009/03/05/a-monad-tutorial-for-clojure-programmers-part-1/
> Oh, wow!  Lucidly written.  And it gives me something, maybe-m, that I can't
> believe I got along without before.  I'm eager now to continue with part 2

Agreed. :-)

Well, I almost gave in to an urge to go on musing about FP in general,
but I decided to suppress it for now and to post instead my own short
bibliography of monad-related Web resources. Clojure-based stuff comes
first, though I'm also including two Haskell links of particular
interest. Michael has already mentioned Konrad's tutorial, but it is
still included below so that I can use this posting as a
self-contained reference.

Clearly it is true that there is no need to learn about monads to
programme in Clojure (and to be productive and have fun while doing
so), but they do make for a very interesting approach to structuring
computations, so if one is already curious about them, then one cannot
go wrong with some monadic food for thought. ;-) (The full immersion
programme is run in Haskell, of course, but the Clojure experience is
very enlightening too.)

All the best,
Michał

Monads: A Bibliography
=================

First, the above mentioned tutorial by Konrad Hinsen:

[A Monad Tutorial For Clojure Programmers][1]

The following articles by Jim Duey are also very good (and they do
take a somewhat different approach to Konrad's tutorial, so it's worth
while to read both series):

[Monads in Clojure][2]
[Higher Level Monads][3]
[Why Use Monads][4]
[The Continuation Monad in Clojure][5]
[Sessions for Compojure][6]

There's also a very good Haskell resource on the most frequently used monads:

[All About Monads][7]

The example code is in Haskell, but each monad's section includes some
motivating discussion.

Then there are [Philip Wadler's monad-related papers][8]. IIRC,
"Monads for functional programming" is a bit of a tutorial paper, so
that may be worth skimming.

And just for the pleasure of reading through the numerous displays of
breathtaking FP brilliance collected therein, take a look at [Oleg
Kiselyov's site][9]. The section labelled "Computation" has a
subsection devoted to monads.

[1]: http://onclojure.com/2009/03/05/a-monad-tutorial-for-clojure-programmers-part-1/
[2]: http://intensivesystems.net/tutorials/monads_101.html
[3]: http://intensivesystems.net/tutorials/monads_201.html
[4]: http://intensivesystems.net/tutorials/why_monads.html
[5]: http://intensivesystems.net/tutorials/cont_m.html
[6]: http://intensivesystems.net/tutorials/web_sessions.html
[7]: http://www.haskell.org/all_about_monads/html/index.html
[8]: http://homepages.inf.ed.ac.uk/wadler/topics/monads.html
[9]: http://okmij.org/ftp/

Per Vognsen

unread,
Mar 19, 2010, 10:41:23 PM3/19/10
to clo...@googlegroups.com
They provide elegant solutions to a great many problems, some quite
simple. Here's how I would compute subobjects (subseqs, subsets),
partitions and permutations monadically:

(def true-or-false (constantly (either true false)))

;; computes subseqs in the seq monad and subsets in the set monad
(defn subs [s]
(m-filter true-or-false s)))

(defn partitions [s]
(m-partition-by true-or-false s))

(def permutations [s]
(when (seq s)
(>> (permutations (rest s))
[[left right] (m-split-with true-or-false %)]
(concat left [(first s)] right))))

You could respond that most of the heavy lifting is done by the
monadic transliterations of commonplace seq functions like filter,
partition-by and split-with. You would be right; that is the whole
point.

Anyway, you certainly don't need to know any of this stuff to code
Clojure, and I do agree that is a strength of the language compared
to, say, Haskell.

-Per

Reply all
Reply to author
Forward
0 new messages