Programming and writing about it.

echo $RANDOM

Category: Functional Programming

Quick Tip: Using the ‘parsec’ Haskell library

You will have to do a sudo apt-get install libghc6-parsec-dev and sudo apt-get install libghc6-parsec-doc on Ubuntu/Debian systems if you are going to use the parsec library with ghc installed via apt-get

Once you have installed both, you should be able to use the library in your Haskell code and also view the library documentation.


Higher-Order Procedures

Higher-Order procedures is my attempt to introduce the topic in an easy way. Please post your comments/suggestions here. (PDF)

Book Review: Practical Common Lisp

Practical Common Lisp is a freely available book on Common Lisp which gives the impression that it can be appealing to the ignorant but enthusiastic newbie to Lisp. Lisp and other languages of this niche category are often thought to be of any use, only in academic circles. This book attempts to dispel that myth by having a ToC which mixes theory and practical applications, both.

I have a working knowledge in C/C++, Python and Java and am knowledgeable of the Functional Programming paradigm. However, I have zero experience with any dialect of lisp.

In this post I shall try to put down my own reviews. chapter wise, as I read and work them.

Chapter 1: Introduction: Why Lisp?

From the book chapter: “If you think the greatest pleasure in programming comes from getting a lot done with code that simply and clearly expresses your intention, then programming in Common Lisp is likely to be about the most fun you can have with a computer. You’ll get more done, faster, using it than you would using pretty much any other language.”

This chapter puts the whole book into perspective and answers the first question that comes to the reader’s mind. A bit of history about Lisp is discussed, along with the author’s personal experiences with Lisp about getting the job done in much fewer lines of code.

Personally, I love picking up a new language from time to time. Lisp has been beckoning me for some time now. Hence, I picked up the book at the first instance.

A good first chapter. Me, the newbie likes it.

Chapter 2: Lather, Rinse, Repeat: A Tour of the REPL

The first taste of Lisp programming is felt in this chapter.

Lisp in a Box packages a Common Lisp implementation- Allegro with the Emacs editor and
SLIME– Superior Lisp Interaction Mode for Emacs. Once you extract the
tarball downloaded, you will find that there is Allegro CL
implementation, Slime and Emacs binaries.

On executing the ‘’ script, you will finally get Lisp prompt:CL-USER>

can also modify the ‘’ script to use your existing Emacs
binaries, instead of the one bundled. Modify the last line, starting
with exec emacs to point to your own Emacs installation. For eg.

exec /usr/bin/emacs --no-init-file --no-site-file --eval='(progn (load "lispbox") (slime))'<br />

The List processing is well introduced with examples such as (+ 2 3), (format t "hello world") . The concept of the return values and side-effects of functions is well demonstrated.

The rest of the chapter takes the reader in a REPL loop, moving from a “Hello, World” value Lisp code to a “Hello, World” program to the concepts of functions in Lisp. The reader also learns to save his Lisp programs and load use them in later sessions. The Lisp debugger is also mentioned.

new to Emacs are made familiar some rudimentary Emacs commands to move
around, save files, compile and re-compile Lisp sources.

Along with being a introductory chapter on Lisp programming, the reader almost has a feeling that he is learning Emacs as well, which is double whammy :-)

UPDATE: More than being a double whammy, I think its more of a hindrance to try and learn Emacs along with Lisp, even though the two do go hand-in-hand. You are better off just using the Allegro CL Environment for the code editing and execution.

All set for the next chapter.

Chapter 3: Practical: A Simple Database

This chapter is the book’s first Practical chapter in which you write some CL code to design a useful working application for you. Here, the reader code a MP3 database for a personal music collection.

This is the first post Hello World program and language features such as the fundamental list, Lisp operators- functions, macros, Property lists– simplified, but useful stripped down hash table-like data structure, Simple interactions with the user, loading and saving files on disk.

Lambdas, or anonymous functions are introduced and used widely in the code.

I come from a background in C/C++, Python, Java where one of the things I essentially do is call a function and use its return values in variables or placeholders. What I learnt in this chapter, is to design a program around functions and lambdas (anonymous functions). Yes, Python has lambdas, but I haven’t learnt to design my program around it, till now.

Another concept that I really found new is the concept of macros in Lisp:

“Common Lisp macro shares
essentially nothing but the name with the text-based macros found in
C and C++. Where the C pre-processor operates by textual substitution
and understands almost nothing of the structure of C and C++, a Lisp
macro is essentially a code generator that gets run for you
automatically by the compiler. When
a Lisp expression contains a call to a macro, instead of evaluating
the arguments and passing them to the function, the Lisp compiler
passes the arguments, unevaluated, to the macro code, which returns a
new Lisp expression that is then evaluated in place of the original
macro call.”

Pure enlightenment.

Somwhere in my mind, now I have started thinking about a program as independent blocks of symbols and operators with the order of evaluation being determined by the parenthesis. I think I will be able to write this in a more sensible way as I learn more of this.

As a side note, I would highly recommend you to read this article: The Nature of Lisp

Chapter 4: Syntax and Symantics

This chapter helps you to sink in all that has been introduced in the previous chapter. It introduces the basic paradigms and concepts behind a Lisp program with just the right amount of detail.

The burning question: What’s with All the Parentheses? is addressed in this chapter. Why does such a syntax make sense? This is explained by talking about how the execution of a Lisp program differs from the stages of execution of a program coded in other languages in Breaking Open the Black Box.

S-expressions and symbols– functions, macros and special symbols are all explained in detail. The usage of macros in the previous chapter is explained in greater detail here.

The reader also gets to know about how all (almost) Lisp operations are evaluated as functions and all expressions are Lists in Lisp. Thus, appreciating all the parenthesis that surrounds the code.

The chapter concludes by looking at Truth, Falsehood, and Equality in Lisp.

Chapter 5: Functions

Functions, which is one of the three basic components of Lisp programs (others being, macros and variables) are given detailed treatment in this chapter. After using them in chapters 2 and 3, this chapter looks at the various components of a function in great detail.

Different kinds of parameters that a function can take is discussed here.

Returning from functions, Higher Order functions, Lambdas (Anonymous functions) are the other things discussed.

Chapter 6: Variables

This chapter takes a look at another basic building block of Common Lisp- variables.
Lexical and Dynamic variables and their features are discussed in detail. Closures are discussed for the first time in this chapter.

Basic operations on variables are demonstrated.

Chapter 7 and 8: Macros: Standard Control Constructs, Defining your own

In these two chapters, the author writes about Macros in Common Lisp. In chapter 7, the author looks at the standard control construct macros defined by Common Lisp. This chapter helps the reader to understand a lot about how macros are used, almost everywhere in the language itself in Common Lisp.

The next chapter builds upon the former to help the reader to write a trivial, but useful macro on his own, introducing concepts along the way to write code that writes code.

Chapter 9: Practical: Building a Unit Testing Framework

In the second Practical
chapter, the author helps the reader code a simple Unit Testing
Framework, mostly making use of some of the concepts introduced in the
last couple of chapters on Macros.

Chapter 10: Numbers, Characters and Strings

As the name of the chapter suggests, this will be one of those reference chapters you will coming back to, as you use more of numbers, characters and strings in the Common Lisp language.

From here onwards, you will notice that, reviews for later chapters appear earlier and vice versa.

This indicates a couple of things:

  1. That I am reading the chapters, which I find interesting first and then coming back to some earlier chapter which might have been referred to in there
  2. More importantly: Most of the basics have been covered till Chapter 10

Chapter 21: Programming in the Large:Packages and Symbols

As is apparent, this chapter is a first step in the world of writing and using medium to large scale Common Lisp codes. As you start coding you may want to help the world by creating and distributing your own packages or use a lot of third party packages which provide Common Lisp features which may not be available in a standard implementation. This chapter takes on a tour of packages and a detailed look into how the REPL resolves symbols.

A must read before moving on to the later chapters.

Chapter 14 and 15:

Files and File I/O and the next Practical chapter, as their names suggest looks at working with files- text and binary using Common Lisp.

These chapters introduce important concepts such as Pathnames which help abstract Operating System level differences in the way files are referred to. The practical chapter builds upon Pathnames to code up a truly portable pathname library using *FEATURES* to work across several Common Lisp implementations. This pathname package will be used in later chapters to code up other practically useful tools.

Post last updated on March 10, 2009