# LoLClojure – Locals

Just like in Lisp, Clojure uses `let` to define locals. The only real difference is that Clojure uses a vector of names and their bindings, whereas Lisp uses a nested list.

This Lisp code

```(let ((a 5)
(b 6))
(+ a b))
```

looks like this in Clojure

```(let [a 5
b 6]
(+ a b))
```

I think the Clojure way is a little easier to read.

The biggest difference between the two is when it comes to local functions. CL has `flet` for defining local functions, and `labels` for defining local functions that need to be able to call each other (or call themselves recursively). Here’s an example of each

```;; A single local function
(flet ((f (n)
(+ n 10)))
(f 5))

;; Two local functions that don't need to call each other
(flet ((f (n)
(+ n 10))
(g (n)
(- n 3)))
(g (f 5)))
```

Here’s the equivalent code in Clojure. Note how the square brackets make things stand out a little bit more.

```(letfn [(f [n]
(+ n 10))]
(f 5))

(letfn [(f [n]
(+ n 10))
(g [n]
(- n 3))]
(g (f 5)))
```

If the functions need to reference each other, in CL you have to use `labels`, instead of `flet`. Here’s how that looks (the only difference is the form used; the arguments remain the same)

```(labels ((a (n)
(+ n 5))
(b (n)
(+ (a n) 6)))
(b 10))
```

In Clojure, you don’t need to use anything other than `letfn`, because it already supports the recursive nature that `labels` provides

```(letfn [(a [n]
(+ n 5))
(b [n]
(+ (a n) 6))]
(b 10))
```

Finally, if you have local functions and other local bindings you need to establish, you can use a `let`, but no recursion is supported. This is sort of like CL’s `flet` but you can also use it for binding locals that are not functions

```(let [a (fn [n]
(+ n 5))
b (fn [n]
(+ (a n) 6))
c 10]
(b c))
```

I think the way Clojure uses square brackets in certain places that CL uses parentheses makes the code easier to read, overall.

# LoLClojure – Land of Lisp In Clojure

I read Conrad Barski’s excellent book Land of Lisp a couple of years ago, and worked through all the examples using CLisp, but I thought it might be fun to go through it again, but use Clojure instead. Other people have done it already, but what’s one more, eh?

As I work through the book, I will be putting all the code on Github at https://github.com/joeygibson/lolclojure

So, the first example is for a program to guess a number you are thinking of. In Lisp, `defparameter` allows you to rebind values, but Clojure’s `def` is immutable. Using a `ref` gets around this, though it is a bit clunky (since `ref`s are intended for multi-threaded access.) The code is not great, and you wouldn’t write a Clojure program like this (or a Lisp program, really); it’s just to get the discussion moving. Better code is coming.

Anyway, here’s the number-guessing program in non-idiomatic Clojure. To run it, load it into a REPL, then execute `(guess-my-number)`. If you are so enraptured with the game that you want to play it again, execute `(start-over)` and then `(guess-my-number)`.

```(ns lolclojure.guess)

;; Using refs for these is overkill, but the original
;; used mutable global variables.
(def small (ref 1))
(def big (ref 100))

(defn guess-my-number
"This is, effectively, a binary search between the two extremes."
[]
(Math/round (Math/floor (double (/ (+ @small @big) 2)))))

(defn smaller
"The guess was too high, so lower it."
[]
(dosync
(ref-set big (dec (guess-my-number)))))

(defn bigger
"The guess was too low, so raise it."
[]
(dosync
(ref-set small (inc (guess-my-number)))))

(defn start-over
"Reset everything and prepare to start over."
[]
(dosync
(ref-set small 1)
(ref-set big 100)))
```

# George Carlin On Flamethrowers

My favorite George Carlin quote of all time is this:

The very existence of flamethrowers proves that some time, somewhere, someone said to themselves, “You know, I want to set those people over there on fire, but I’m just not close enough to get the job done.”

I laugh like a schoolgirl whenever I think about it. And here’s a video of him doing this bit, though worded slightly differently:

# Get a REPL On Your Current Java Project

I like to test things out interactively, so I love working with languages that provide a REPL. I’m currently working on a Java project, but Java doesn’t have a REPL. Several languages built on top of the JVM do have them, and these langauges can access the Java classes on their classpaths. Groovy, Scala and Clojure are just three such examples, that I happen to work with.

I got this tip from this response on a Stackoverflow.com post. His tip was for Scala, which looks like this:

```scala -cp target/classes:`/usr/bin/mvn \
dependency:build-classpath \
| grep "^[^\[]"`
```

The bit between the backticks runs a Maven goal that outputs the jars that your project depends on, and then extracts just the list of fully-qualified jar files to append to the Scala classpath. If you want to use Groovy for your REPL, it would look like this:

```groovysh -cp target/classes:`/usr/bin/mvn \
dependency:build-classpath \
| grep "^[^\[]"`
```

Similarly, if you’d rather use Clojure, do this:

```java -cp target/classes:`/usr/bin/mvn \
dependency:build-classpath \
| grep "^[^\[]"`:/path/to/clojure.jar clojure.main
```

(Note: In the examples above, I’ve split the code across multiple lines for clarity. In reality, it can be all on one line.)

# That Insipid “Ancient Aliens” Show

O, how I hate that stupid “Ancient Aliens” show on the “History” channel. Tonight, as I was scrolling through the channel guide, I happened to see the info for an episode that was about to go off. The title of this episode was, “The Satan Conspiracy”, and the description was

Ancient texts indicate that Satan may have been an extraterrestrial…

Um… yeah, if you believe what the bible says about Satan, that he exists at all, and where he came from, then of course he’s an extraterrestrial. Because what is an extraterrestrial? According to dictionary.com, it’s, “outside, or originating outside, the limits of the earth”. That sounds like what the bible says about Satan, God, angels, pretty much everyone who wasn’t created on the Earth. Of course, I think Satan, “the devil”, “Beelzebub”, etc., are just allegories for evil, and not an actual being, but this still really annoys me.

I’m really ready for idiotic shows like this (and those celebrating semi-literate rednecks) to run their course and go away.

# Conway’s Game of Life, In Go

About a week ago, I decided to implement Conway’s Game of Life in Go. There was no particular reason, other than that I was bored, I wanted to do something else with Go, and I had never tried to implement Life before.

For those who don’t know, Life is a zero-player “game” in which an ecosystem is seeded, and then plays out all by itself. Once begun, each generation follows these basic rules:

1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.
2. Any live cell with two or three live neighbors lives on to the next generation.
3. Any live cell with more than three live neighbors dies, as if by overcrowding.
4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

That’s it. You start it up, and then sit back and watch for patterns to emerge. So far, I have a version that uses a portable curses-like library for console graphics. It runs on OSX, and should definitely run on Linux. The page for termbox-go says that it will run on Windows, but I haven’t tried it. I plan to build a “prettier” version using Gtk+, but that will take a little while, since I don’t know Gtk+ (yet!).

Here’s a screenshot of the current version running:

If you want to see the code, it’s on github.

# Now I Know: Aces High

From today’s edition of the Now I Know newsletter, this bit of interesting stuff:

In ordinal rank, the ace in a deck of playing cards is lower than the two — it’s the one, after all. But in most card games, the ace plays as the high card, trumping the King. Why? According to Wikipedia, this use became popular after the French Revolution, with the ace symbolizing the rise of the commoner over the monarchy.