For more than a decade now, I have been paid to do what I love, write code. During all this time I’ve been using quite a few different languages and web frameworks.
I’ve been thinking about learning a functional programming language for quite some time now and I started out with Erlang but I could never wrap my head around the syntax.
And then, out of the blue, a friend of mine told me about the book “Land of Lisp” and how it would change my life… OK, slight exaggeration.
The name Lisp derives from “LISt Processing”. Linked lists are one of Lisp languages’ major data structures, and Lisp source code is itself made up of lists.
The first version of Lisp was released in 1958, some 35 years before the initial release of Ruby. It was introduced in the era of FORTRAN and COBOL.
Land of Lisp
With its eye-catching subtitle Learn to Program in Lisp, One Game at a Time! you immediately get the feeling that this isn’t your normal, run-of-the-mill programming book. And it turns out to be something completely different.
(Hint: It’s a comic book in the same vein as Why’s (Poignant) Guide to Ruby)
I have a repo on GitHub where I will push notes and code while I read the book.
All of the examples in “Land of Lisp” are written in Common Lisp, but I actually started out by playing with a younger Lisp dialect; newLISP.
newLISP is a scripting Lisp for people who are fascinated by Lisp’s beauty and power of expression, but who need it stripped down to easy-to-learn essentials.
If you are using OS X and have Homebrew
installed, then you can install newLISP with the following command:
brew install newlisp
Lisp is using prefix notation, also known as Polish notation.
Most programming languages are limited to two operands per operator but Lisp can have any number of operands per operator.
Compute the sum of 4,2,1
; newLISP (+ 4 2 1)
# Ruby [4,2,1].inject(:+)
Check if 4 is greater than 3, and 3 is greater than 2
; newLISP (if (> 4 3 2) (println "4 is greater than 3, and 3 is greater than 2"))
# Ruby puts "4 is greater than 3, and 3 is greater than 2" if 4 > 3 && 3 > 2
Learn more about newLISP
A collection of modules for a variety of things are available over at GitHub (Including the dependency-managing library QWERTY).
Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard document ANSI INCITS 226-1994 (R2004)
You can install it using Homebrew:
brew install clisp
Quicklisp is a library manager for Common Lisp. It works with your existing Common Lisp implementation to download, install, and load any of nearly 700 libraries with a few simple commands.
You will find a lot of examples in the article Features of Common Lisp.
; CLISP (/ 1 2) ; => 1/2
# Ruby (1.0/2).to_r # => (1/2)
Multiple return values
; CLISP (floor pi) ; 3 ; ; 0.14159265358979323851L0
Conveniently, functions that return multiple values are treated, by default, as though only the first value was returned:
; CLISP (+ (floor pi) 2) ; => 5
To achieve something similar in Ruby (slightly contrived example):
# Ruby class MultiValue < BasicObject attr_reader :secondary def initialize(obj, *secondary) @obj, @secondary = obj, secondary end def method_missing(sym, *args, &block) @obj.__send__(sym, *args, &block) end end class Array def to_mv MultiValue.new(*self) unless empty? end end class Numeric def floor_with_remainder [floor, remainder(floor)].to_mv end end
This allows us to do the following:
# Ruby Math::PI.floor_with_remainder + 2 # => 5 mv = Math::PI.floor_with_remainder mv.secondary.inject(mv, :+) # => 3.141592653589793
If you want to learn more about writing Lispy Ruby, then you should take a look at Chapter 8 in the book Practical Ruby Projects: Ideas for the Eclectic Programmer by Topher Cyll.