Slowly getting the hang of Clojure

| geek

At least in terms of 4clojure problems. =) I've worked my way through the elementary and easy problems (woohoo!), with 88 exercises completed so far. It took me a while to get the hang of lazy-seq, since Emacs Lisp doesn't do lazy evaluation. Once I started thinking of it as a macro, though, I finally got the hang of it. Here's my solution for the exercise for reimplementing map:

(fn my-map [func input]
      (if (sequential? input) 
        (cons (func (first input))
              (lazy-seq (my-map func (next input))))))

There are more exercises to do, and it's fun to feel the different concepts start coming together. I might not do the hard exercises, though. I still haven't thought of any practical purposes I'd like to use it for, though…

You can view 4 comments or e-mail me at sacha@sachachua.com.

4 comments

Norman Richards

2014-06-11T15:27:10Z

Good work! Laziness takes a surprising amount of work.

One small issue - you really want to l test (seq input) instead of (sequential? input) because what you really want to know here is if input has any items. (sequential? []) is true, so if you call (my-map identity []) you will get (nil) instead of () or nil, which is what you would expect when you map over an empty list.

I hope you keep up the Clojure - it's a great language!

Many thanks! I wasn't sure if seq was the right function because it didn't look like a test, but I see now from the docs that seq returns nil if the collection is empty. =)

octopusgrabbus

2014-06-11T19:24:39Z

As a suggestion you need a project. That will provide you with a goal. I did this about three years ago. It was to replace some Perl code with Clojure. The code sent and received data using http. I got a lot of help from the internet; struggled; and eventually got the project completed. But, I learned a lot.

I'll just have to override my "Do everything in Emacs or Ruby!" thing, then... =)