Learning Lisp

The Road To 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.


5 Responses to “Beginning of Bag Project”

  1. Zach Beane said

    The accessor names are a little funky. I would call the accessor for the items in the bag ITEMS and the accessor for the maximum weight MAX-WEIGHT. There’s also no real need to call the slot ITEMLIST; just ITEMS would do. (If you change from a list to, say, an extensible vector in the future, there will be no need to rename your slot.)

    Along a similar line, I would rename *LIST-OF-BAGS* to just *BAGS*.

    GET-WEIGHT could be named simply WEIGHT. Also, using EVAL is a little silly; you could use REDUCE there.

    ADD-ITEM will have no effect on the items in the bag.

    Generic functions are at the heart of object-oriented programming with CLOS. Methods are contained within generic functions, rather than being associated with a specific class. When you use DEFMETHOD without DEFGENERIC, a generic function is implicitly created to contain it. Creating a generic function first makes it somewhat easier to document your intent.

  2. Vleis said

    I found the original problem you posted kinda interesting so I whipped up the following recursive function to grab integers out of a list that add up to 20 (or less if there is no possibility of getting 20):

    (defun find20 (list curval)
    (if (>= (length list) 1)
    (if (

    You can call it like so (using the list your teacher provided):

    (find20 '(15 13 4 9 6 1) 0)

    which generates the following output:

    (15 4 1)

    If I now remove the above three integers from the original list and run it through find20 again:

    (find20 '(13 9 6) 0)

    I get:

    (13 6)

    I am also still just learning lisp and getting my head around functional programming and recursion in general, hopefully you can use some of this 🙂

    (I'm kinda irritated by the fact that I have no nicer way of passing the running count of items collected so far except via the curval parameter, obviously there is room for improvement still)

  3. Vleis said

    jeez, lets try this again, the function looks like this:

    (defun find20 (list curval)
    (if (greater-than-or-equal (length list) 1)
    (if (less-than-or-equal (+ curval (car list)) 20)
    (cons (car list) (find20 (cdr list) (+ curval (car list))))
    (find20 (cdr list) curval))))

    I think the greater than and less than symbols screwed up my previous posting, just replace the above instances of greater-than-or-equal and less-than-or-equal with their correct operators.

  4. […] Ah well. That particular blogger only lasted a mere two months before fading into the sunset. His final post included some original code for solving a programming problem and resulted in some helpful comments […]

  5. […] This reminds me of a previous post in which I discuss “mind control” as a form of controlling the energy flowing through our brains.u00a0 It demon Click https://twitter.com/moooker1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: