My Clojure explained solutions to the s99 problems 21 to 26

This post will present and, I hope, explain my solutions to the s99 problems 21 to 26. I’ve already posted the solutions and explanations for the problems 1-3, 4-7, 8-13 and 14-20.

P21

Insert an element at a given position into a list.
Example:
user> (insert-at(\c, 3, (list \a, \b, \d))
(\a, \b, \c, \d)

Read more of this post

My Clojure explained solutions to the s99 problems 14 to 20

P14

Duplicate the elements of a list.
Example:
user> (duplicate ‘(\a, \b, \c, \c, \d))
(\a, \a, \b, \b, \c, \c, \c, \c, \d, \d)

Read more of this post

My Clojure explained solutions to the s99 problems 8 to 13

P08

Eliminate consecutive duplicates of list elements.
If a list contains repeated elements they should be replaced with a single copy of the element. The order of the elements should not be changed.
Example:
user> (compress ‘(\a, \a, \a, \a, \b, \c, \c, \a, \a, \d, \e, \e, \e, \e))
(\a, \b, \c, \a, \d, \e)

Read more of this post

My Clojure explained solutions to the s99 problems 4 to 7

In this post, I’ll continue presenting my solutions for the s99 problems in Clojure. As a reminder, I’ve already covered the first 3 problems in this post.
Read more of this post

My Clojure explained solutions to the s99 problems 1 to 3

A year (or maybe more) ago, I started toying with emacs and soon had to tweak its configuration file, which is actually a program written in elisp, a variant of the lisps family.

I had no former experience with anything like lisp before, and anytime I touched the .emacs file, a pile of weird error messages would show up when starting emacs. So I thought it might be a good idea to learn the lisp’s syntax so I’d at least know what was that unbalanced quote I kept seeing in .emacs snippets in the internets.

With time, I started appreciating more and more lisp’s minimalistic syntax, and here I am now, spending more and more time toying with Clojure, a modern Lisp variant that runs on the JVM (my platform of choice, being a Java developer for a couple of years already).

Before that, I learned me some Scala, and to help myself get rid of my imperative programmer reflexes (for loops, variables, etc.), I started coding the solutions for the excellent s99 problems using Scala. It was a joyful although painful experience: Painful because my mind would find the imperative solution first, but I had to refrain from using it while knowing that Scala’s constructs would let me do it, and rather keep reminding myself that the functional solution would have no variables. Joyful because I had to challenge my thought process to not pick the familiar path and tackle problems differently.

Now that I’m trying to learn Clojure, I figured I’d do the same and solve the s99 problems using Clojure. The problems are generic enough and not tied to any specific language.

In the following you’ll find the first 3 problems and their (hopefully) explained solutions.

Disclaimer: I’m still in my baby steps with Clojure and did the best I could with the little I know so far. I would be glad to hear the other clojurists opinions on my approach and any constructive criticism.

Read more of this post

Follow

Get every new post delivered to your Inbox.