Learning Lisp

The Road To Lisp

Archive for the ‘Bag Project’ Category

Posts having to do with the Bag Project conversion from Java->Lisp

Beginning of Bag Project

Posted by j1p on May 5, 2006

So I started working on implementing the Bag project in Lisp.

I've got most of the basics of the class done, in the following code:

 (defclass bag ()
    :accessor get-bag
    :initform ()
    :initarg :itemlist
    :documentation "A list of items in the bag")
    :accessor get-bag-max-weight
    :initform 20 :type integer
    :initarg :max-weight
    :documentation "The maximum weight that the bag can contain")))

(defvar *list-of-bags*
  "List of Bags")

(defun make-bag (&optional (itemlist ())(max-weight 20))
  "Makes an instance of class bag and pushes it into *list-of-bags*"
  (setf *list-of-bags*
     (make-instance 'bag :max-weight max-weight :itemlist itemlist)

(defmethod bag-weight ((bag bag))
  (eval (cons '+ (get-bag bag))))

(defmethod add-item ((bag bag) (item integer))
  (cons item (get-bag bag)))

I still need to implement the function to go through a file, and put all integers in the file into the first bag that it will fit in, and the function to print out the bag. I'm not familiar with reading from files in Lisp, but I assume it should be relatively simple. I've skimmed through the chapter on files and file-io of Practical Common Lisp, and it mentions two functions, read-char, read-line, and read, and it looks like the one that will be most helpful to me will be read. The loops to go through and print/pack the bags will probably use a FIRST of a NTHCDR of *list-of-bags*.

Please, if you have any suggestions/fixes for what I have, let me know, im just learning and don't want to start up any bad habits. For example, I haven't exactly seen the reason to have defgeneric's instead of defmethod's, but I'm sure there is one.


Posted in Bag Project, Lisp | 5 Comments »

Re: Don’t Study, Do.

Posted by j1p on May 3, 2006

I think the Lisp bug has bitten me.

After an uneventful morning, consisting of daydreaming through class and a quick lunch, I rushed to get to work on time. Now, I really do enjoy working here, I tend to look forward to the 3 hours a day I get to spend on a computer, hacking away(normally at html, ColdFusion, or CSS) without being interrupted by neighbors, etc, but I'd rather be doing some other things. I get to work, and to my surprise my 3 co-workers all seemed to have taken the day off. I got through all the webteam stuff in 30 minutes, so I figure I might as well read up a bit on Lisp.

While browsing technorati, I came across a post, How To Advance in InfoSec: Don't Study, Do. While this post has little to do with Lisp in general, it is extremely relevant to what I'm doing here. Here's a quote straight from the post(actually the only part that even mentions Lisp):

Don’t read three books in a row on Lisp. Take a program you’ve written in a previous language and redo it in Lisp. Use your three books as a reference.

That was exactly what I was doing, I checked out 3 books on Lisp, and was reading through them like a tutorial. I need some real experience working with Lisp, making it do something I want it to do, not just what they tell me it can do.

So, I'm going to try to rewrite my first CS102 assignment from Java to Lisp. Here are the details as posted by my professor.


  1. You need to create a class called Bag that contains an array of items. You may assume that the items are all integers (the weights of the various items in pounds) and that the capacity of the bag is twenty pounds. Your private variables will include this array of integers, a count of how many items are in the array, and any other data you feel you will need. The methods for the class should include the any constructors that you will need, any query functions (such as examining the weight currently held in the bag), and a method to insert a new item into the bag. You may, of course, include any other methods that you feel you need.
  2. Your input will be a list of integers. You may read it in from the screen or from a text file (a text file is probably easier). If you read it in from a text file, be sure to prompt the user for the name of the file that is to be used for the input. If you input the weights from the screen, be sure to give the user sufficient prompts. If any bad data (such as a character) is in the input, ignore it and proceed to the next input.
  3. If your input is: 15 13 4 9 6 1 , for example, your output should be of the form:

    Bag 1: 15 4 1

    Bag 2: 13 6

    Bag 3: 9

  4. You should create a second class for packing the bags that contains a method called pack(), which reads in your data and packs them into the bags. You will need a method to print your results, which calls a print method in the bag class. Your main method should be very short, essentially calling pack() and print().

  5. Be sure to include comments in all of your methods

Alright, so this looks like it is going to be much simpler in Lisp than in Java, for the fact that practically all it is is list manipulation, Lisp's specialty. I have no need to make the Bag class contain an array of items, it can simply be a List of items in Lisp.

To make this work as close to the specifications as I can, I am going to need to learn more about CLOS, though most of the methods for the classes look like they will be pretty simple.

I have a 10 page paper to write for class tomorrow, but I'll probably hold that off until after I get home and use SLIME to write up some preliminary functions that will help for the final Lisp-version of Bag.

Posted in Bag Project, Learning, Lisp | Leave a Comment »