Learning Lisp

The Road To Lisp

Archive for April, 2006

Use the Library

Posted by j1p on April 24, 2006

Instead of just using online resources like Peter Seibel's Practical Common Lisp [1], I headed down to my University's Library and took out 3 books on Common Lisp. I was originally looking to get a copy of Paul Graham's ANSI Common Lisp[2], but they didn't have it.

So instead I picked up Guy L. Steele's Common Lisp: The Language[3], Winston and Horn's Lisp[4], and Andreas Paepcke's Object-Oriented Programming: The CLOS Perspective[5]. These books may be a bit outdated, but should be good for getting my feet wet with Common Lisp. So far I've only read through Lisp's 1st chapter, and half of the second chapter. The first chapter involved no code, only explanations of lists, uses of Lisp in a real-world environment, why to learn Lisp(especially Common Lisp), and debunked a few myths about Lisp. Nothing I haven't read elsewhere.

The second chapter finally got into real code. It introduced primitives, procedures provided by the Lisp implementation, like arithmitic operators, cons, list, append, first(car), rest(cdr), and setf. Further along in the chapter it goes into push, nthcdr, butlast, last, length, reverse, ASSOC, round, exp, expt, and so on.

For anyone also learning I'll provide the sample problems I've come across so far:

Problem 2-1: Identify if the following are atoms, lists, or neither:

  1. ATOM
  4. ((A B) (C D)) 3 (3)
  5. (LIST 3)
  6. (/ (+ 3 1) (- 3 1))
  7. )(
  8. ((()))
  9. (() ())
  10. ((())
  11. ())(
  12. ((ABC

Problem 2-2: Evaluate the following forms:

  1. (first '(p h w))
  2. (rest '(b k p h))
  3. (first '((a b) (c d)))
  4. (rest '((a b) (c d)))
  5. (first (rest '((a b) (c d))))
  6. (rest (first '((a b) (c d))))
  7. (rest (first (rest '((a b) (c d)))))
  8. (first (rest (first '((a b) (c d)))))

Problem 2-3: Evaluate the following forms:

  1. (first (rest (first (rest '((a b) (c d) (e f))))))
  2. (first (first (rest (rest '((a b) (c d) (e f))))))
  3. (first (first (rest '(rest ((a b) (c d) (e f))))))
  4. (first (first '(rest (rest ((a b) (c d) (e f))))))
  5. (first '(first (rest (rest ((a b) (c d) (e f))))))
  6. '(first (first (rest (rest ((a b) (c d) (e f))))))

Problem 2-4: Write a sequence of first's and rest's that will return the symbol PEAR out of the following expressions. (not the list containing only the atom PEAR, (PEAR), but only the symbol PEAR, PEAR)

  1. (apple orange pear grapefruit)
  2. ((apple orange) (pear grapefruit))
  3. (((apple) (orange) (pear) (grapefruit)))
  4. (apple (orange) ((pear)) (((grapefruit))))
  5. ((((apple))) ((orange)) (pear) grapefruit)
  6. ((((apple) orange) pear) grapefruit)

Hint: First returns a element, while Rest always returns a list.

Problem 2-5: Evaluate the following forms:

  1. (append '(a b c) '( ))
  2. (list '(a b c)'( ))
  3. (cons '(a b c) '( ))

Problem 2-6: Evaluate the following forms in the order given

  • (setf tools (list 'hammer 'screwdriver))
  • (cons 'pliers tools)
  • tools
  • (setf tools (cons 'pliers tools))
  • tools
  • (append '(saw wrench) tools)
  • tools
  • (setf tools (append '(saw wrench) tools))
  • tools

Problem 2-7: Evaluate the following form:

  • (cons (first nil) (rest nil))

I had some trouble on some parts of 2-4, where my sequences would result in the list containing the symbol pear, (PEAR), but that's easily solved by throwing another first on the front of the sequence.

As I continue reading I'll try to keep putting in the example problems, as long as they don't get too involved, and the answers will be in the next post.

One of the problems, well, maybe not so much a problem, but a point of conflict I have run into is which implementation of Common Lisp I should use. Currently, I am running Ubuntu Linux, so I have lots of options to choose from: SBCL, CMUCL, CLISP, Allegro Personal 7.0, and more. Perhaps my next post will deal with the differences between different implementations, in the eyes of a "newbie."

Posted in Learning, Lisp | 10 Comments »