(LISP): ash, binary, decimal….

LISP practice

I needed to find a new resource to learn LISP as the online tutorial I had been using was taken down. Enter the book: Land of LISP. Truth be told, I was going to buy the book a while ago, but the $35 price on Amazon seemed expensive. Thankfully, a message posted in a Slack channel I belong to mentioned that the Humble Bumble Book Bundle for this month was really good. Among the titles for this month is Land of LISP, along with several other books depending upon the price you want to pay and donate.  I choose the $15 option and now have several books, all loaded on various devices – what a great resource to build a tech library while doing good and not ending up with an endless amount of books to store.

Land of LISP – Chapter Two

One of my goals for the day was to complete chapter two, entitled “Creating Your First LISP Program.” While this is definitely not my first LISP program, the chapter had some surprises. The goal of the chapter was to create an interactive number guessing game.

The chapter introduced many building blocks for LISP, which was a bit of a refresher. For example, defparameter (creates a global variable but will overwrite a previous entry), defvar (creates a global variable but without overwriting a previous entry), setf (to change the value of a variable), let (local variable creation – I like that you can declare multiple variables at once), defun (creates a global function) and flet (local function creation – I really like that you can declare multiple functions at once.)

The unexpected element was the algorithm used to determine the next number to guess – it was a binary search! The program used ash (arithmetic shift function). The equation for what ASH does (it uses floor math) looks like this: integer * 2 ^ count. Since I have not thought about the decimal system versus the binary system in years, I spent some time practicing converting binary numbers to decimals and then converting decimal numbers to binary.

Essentially, the algorithm for the number guessing game starts by setting the biggest number to 100 and the smallest number to 1; as you will soon see, this means that the first guess is always 50. Based upon whether you enter smaller or bigger, e.g. to indicate that the number you chose is either smaller or bigger than the program’s guess, it will redefine the biggest number to the guess minus one (smaller) or the smallest number to the guess plus one (bigger), sum the smallest and biggest numbers and then run the ash function on the total, with a count of -1. The result is returned as the next guess.

The guessing game itself is simple, but learning a new way to implement it was not so simple.


Comments are closed.

Blog at WordPress.com.

Up ↑