3.4

Updates:
  1. none
The ANIMAL LISP program

Refer to the Checklist to see if you have completed all of the steps in this assignment.

Here is the Online LISP Manual at Carnegie Mellon. The manual is very dense, but all the functions are listed, and many are quite simple.

Here is the sample Hello World program.

Here is the Mac page for those using Macintosh computers.

Running the basic animal.lisp program

  1. Follow the insructions (see class main web site) for installing Java and downloading the abcl.n.n.jar file into some directory on your machine like C:\csc250\lisp
    [Note: the current Armed Bear LISP file name is abcl-0.15.0.jar but this may change. There is no harm in making copies of the abcl jar file and having it in more than one direcory.]
  2. Copy the animal.lisp program to your lisp directory.
  3. Start up a command shell under your operating system. (Windows: [start, run, CMD], or find Command-Prompt under accessories. MAC: Terminal Window )
  4. You must have a copy of both abcl.n.n..jar and animal.lisp in your LISP directory for the following commands to work.
  5. Start LISP under java: [csc250\lisp> java -jar abcl.n.n.jar]
  6. Load the animal program
  7. CL-USER(1): (load "animal.lisp") [Make sure it is not "animal.lisp.txt"]
  8. Follow the instructions to play the animal game.

Other LISP things you might want to do.

  • Note: LISP is the mainstay language of AI. It is a very powerful programming language. LISP stands for LISt Processing Language.
  • (load "someFile.lisp") will load a LISP programming file into your running LISP envronment.
  • In general, LISP has the format (SOME-FUNCTION argument-one arg-two...) so that the name of the function or command you want LISP to execute is the first thing in the list, followed by the arguments that you want to pass to the function. LISP will act on your command, possibly create some side effects, and return a value back to the screen (or back to some other function that called it).
  • (load "SomeFileA.dat") would load a data file into lisp.
  • (quit) usually exits from LISP gracefully
  • (dribble "SomeFileA.drb") will start recording everything you do, writing it into a file so that you can review it, print it, share it, and submit it, later.
  • (dribble) stops the recording mode, and closes up the dribble file. The dribble file is now just another text file on your disk. Use the dribble technique to show your work in LISP in preparation for submission to COL as part of your assignment.
  • Use the convention SomeFileA.drb, SomeFileB.drb, SomeFileC.drb, and so on so that you make archives and never lose anything. You can always manually delete files later. You should use this convention for all your development files: MyAnimalA.lisp MyAnimalB.lisp and so on.
  • If you make a mistake in the LISP environment, you will "go down" one error level. In general this does not matter much for what we are doing, but usually LISP will tell you how to return to the top level. [In Armed Bear LISP, usually just type "0"]
  • We will use the (defun ...) command to define new functions, and the (setq ...) command to set the value of variables.

Editing your ANIMAL program file

  • To complete this assignment, you will have to be able to edit the existing animal.lisp file, changing it from its original state into a new program that is somewhat different.
  • The general idea is that you make small changes, one at a time, then restart LISP, load the new version of your animal program, and see how it runs. Then, repeat these same steps, making small changes each time.
  • Use versions when making changes to your running LISP program. In our case we will first copy animal.lisp to aniA.lisp. Then we will move to aniB.lisp, aniC.lisp and so on. Each time, test your program to see if it still runs. If not, erase the bad version and go back to the previous version. This will save time!
  • To edit LISP programs you MUST use plain ascii text. You cannot load word processing (e.g., .doc) files into LISP. You cannot load web files (e.g., .html files) into LISP.
  • You will need to find a file editor that allows you to read, and write, plain ascii (plain text) files. In most cases, under Windows, I would use the "Wordpad" editor (using save-as "Text Document"), and would avoid Microsoft Word. "Notepad" will always work, but probably will not like the way line-ends are handled, causing all the program text to run together. You can also download, and use, one of many free text editors, such as TextPad or PFE.
  • ALL computer systems have editors that allow the editing of plain ascii (text) files, so MAC users can also find such editors that run on their machines.
  • You will need to save all the versions of your animal program files to the same directory where the abcl.n.n.tar file resides, and where you are running LISP.
  • After modifying your LISP program, use the "save-as" function from the file drop-down menu, and save your file as plain ascii text, or text document, and change the version to whatever is next in your sequence (e.g., change "aniB.lisp" to "aniC.lisp"). It is very likely that you will have to put quotes around the file name [e.g., aniD.lisp ==> "aniD.lisp"], otherwise the editor may save it under the name "animalD.lisp.txt", and the LISP load command will not find it in your directory.
  • You can always check to see which files are in your working directory under the command shell by issuing the command "DIR" at the prompt, or by looking in the directory under windows explorer. Note that all Mac users will also have this same directory listing functionality, and that in a terminal window you would probably issue the "ls" command (for listing files).
  • Some typical editor problems might be some odd characters that you cannot see, but which are being written into your file behind your back. The Microsoft Word program, under Vista, seems to have this problem, for example. If so, try a different editor.

Adding COMMENTS to your animal program

  • All good programers write comments in their code.
  • When learning to write programs, it is always a good idea to put in extensive comments about what the program is doing.
  • In-line comments can be inserted simply by starting the line, in the editor, with one to three semi-colons: ;;;
  • More extensive full-text comments can be inserted by entering the two characters #|, writing as many lines of comments as you wish, then ending the comment section with |#
  • LISP ignores all comments. Comments are just for people to read.

Some ANIMAL program commands

  • When you say you do not want to play any more, the animal program will stop, but LISP is still running. Your animal data is still in LISP's memory. You can run the animal program again by typing (animal).
  • (save-it "AniDataA.dat") will save your existing animal data into a file. [and remember, use AniDataB.dat, AniDataC.dat and so on]
  • If you want to re-load some data into animal, so that you do not have to re-type everything that it already "learned," issue the load command after you have loaded the animal program. E.g. (load "AniDataB.dat")
  • This program is very simple, and does not check for errors. If you make a mistake in entering the name of animals (such as typing in two words instead of one), or the question (such as forgetting the quotes), your data will be corrupted. If this happens, start over, or reload a previous version of your data. Remeber to delete the corrupted data file from your disk. (e.g., > erase AniDataF.dat)
  • Programming hint: When re-defining functions [e.g., such as (setq *shoot-responses*...)] if you place your new definition after the existing definition, then when the file loads your new definition will replace the old one. If you make a big mistake, you can always then just erase your new lines, and the old definition will again be used when the program is loaded.

The Animal Program assignment

  1. For those that care, you can find a complete online version of a textbook on how to write LISP programs at: Successful LISP
  2. Create a new subdirectory called "ani-files" for the assignment files. When you are done with the assignment, copy all the submission files to this directory, zip them all together, and submit them to COL. Do NOT include the abcl.n.n.jar file! In windows you might be able to create this subdirectory when in the Windows Explorer program by using right-click | new | compressed (or winzip) folder.
  3. Play with the animal program. Dribble some output to a file, showing that LISP is running,
  4. make a copy of animal.lisp into a file called aniA.lisp. Later this will become aniB.lisp, aniC.lisp and so on as you continue to make changes. Save backups!
  5. You will be making small changes to the aniA.lisp program [aniB.lisp,...]. Each time you make a change, load the new program into LISP to make sure that it is still running. KEEP YOUR PROGRAM RUNNING. If you make a small change, and it does not run, you can always go back to the previous version that IS running. You will know exactly where the error is because you only made one small change.
  6. Using the Format LISP command change the animal program to print out your name when it starts up. For this to work, you will need to add a single line of code to the aniA.lisp program as follows:
    
    ;;; HERE IS WHERE YOU PRINT YOUR NAME WITH THE FORMAT COMMAND:
    
    (format t "~%~% >> This is Joesph Green's first animal lisp program! ~%~%")
    
    
  7. Load your animal program into lisp. You should see your message appear. Congratulations. You are now an AI programmer.
  8. TestA.drb:
    1. Turn on dribbling to a file called "TestA.drb". Load the animal file. Start the game. Add some animals. Finish the game. Save your data to a file called "TestA.dat". Turn off dribbling to close the TestA.drb file. Quit from LISP. Copy the two files TestA.drb and TestA.dat to your ani-files subdirectory. These will be zipped together, along with some other files, and submitted to COL.
    2. You have now proven many things, e.g., that you have java and LISP running, that can edit an ascii file, that can run LISP programs, and so on.
  9. TestB.drb:
    1. Modify the *normal-lose-responses* responses variable to include some new reponse strings. Strings are included inside the quotes. So, for example, you might want to add "I really hate to lose!" as a possible response.
    2. Modify the *normal-win-responses* responses variable to include some new reponse strings. For example, you might want to add "I really love to get it right!" as a possible response.
    3. Using the dribble technique, as above, run your new program long enough to show the new output (this is random, so it might take some time). Dribble to a file called "TestB.drb". Copy TestB.drb to the ani-files directory.
  10. TestC.drb --- Creating varied "personalities" for your program.
    1. In this part of the assignment we will create several different, consistent, "personality modes" for your program that cause it to give different sets of responses when it has won, and has lost. For example, if it is depressed mode, and has won, it might say, "Well I guess I won, but I usually don't."
    2. First, create the variables *depressed-win-responses*, and *depressed-lose-reponses*, using setq in the same way the original program set the variables *normal-win-responses* and *normal-lose-responses*.
    3. Write two functions: one to set the program's personality to one that is depressed, and one to set the personality back to normal.
      • (defun make-depressed-personality () 
          [YOUR CODE GOES HERE -- use setq]
        'now-depressed)
        
        (defun make-normal-personality ()
          [YOUR CODE GOES HERE -- use setq]
        'now-normal) 
        
        
      • Each of these functions will make use of the following programming construct, which sets the value of one variable to the value of another variable:
        
        (setq *varA* *varB*) 
        Thus...  
        (setq *varA* 16) (setq *varB* *varA*)
         ...would set the value of the variable *varB* to 16.
        
      • The functions that display the gloat and i-lost responses always select some value from the list contained in the variables *win-responses*, and *lose-responses*. But, these variables can be changed to the values of other lists, such as those contained in variables such as *normal-win-reponses*, *depressed-win-reponses*, and *depressed-lose-reponses*.
      • We will use the (defun new-function-name () ...) function to define some new functions. I have provided just a shell for the make-depressed-personality and make-normal-personality functions that do nothing except return the values 'now-normal, and 'now-depressed. That is they do not actually do anything!
      • Use the setq function to create two additional lines of code in each of make-depressed-personality and make-normal-personality functions to handle the case when the player wins, and the case when they lose. This is where you have to think about how to make these changes in the program. . I have given you the clues. The hint is that each function needs only two additional lines, each of which uses setq.
      • Test your functions by issuing them at the LISP prompt, after animal.lisp has been loaded, but BEFORE each time you run the game, or AFTER you have quit the game but are still in LISP. That is, at the LISP prompt you can enter:
        
        CL-USER: (make-depressed-personality)
        CL-USER: (gloat)
        
        or
        
        CL-USER: (make-normal-personality)
        CL-USER: (i-lost)
        
        
      • Here are some hints on the meaning of the gloat function code:
         
        (defun gloat ()
          (let ((i (random (length *win-responses*))))
            (format t "~A~%" (nth i *win-responses*))))
        
        
      • length: Figure out how many items there are in the list *win-responses*.
      • random: However many items there are, pick a random number between 1 and that number
      • let: set the variable i to that number
      • nth i: select the "i-th" item in the list *win-responses*
      • format: print that item out to the terminal

  11. Hint: If you want to change the way gloat gloats, then you have to change the value of *win-responses*.
  12. Hint: you can always see what is in a variable, such as *win-responses*, by typing its name at the LISP prompt. If it has a value, LISP will display the value. If it has not been assigned a value, then LISP will complain.
  13. Write an additional personality make-manic-personality in the same way we have written make-drepssed-personality .
  14. For ease of grading change the string expressions in each of the response lists so that they start with the capical letters N, D, and M. Thus "Hurray for me!" becomes "N: Hurray for me!" making it easy to know that we are dealing with the "normal" personality.
  15. Run your new program, changing the personalities once in a while (after you have stopped playing one game of animal, but before you start the next one) to show that they work correctly. Dribble to TestC.drb.
  16. Copy TestC.drb to your ani-files subdirectory.
  • Add extensive comments to your program code explaining how the program works, and highlighting what you have done.
  • Copy the aniF.lisp program to the ani-files subdirectory.
  • Zip everything in ani-files together, and submit to COL.