To work through these exercises, refer to the above material for more information.
;; A typical print expression (print "Here is some text")
Note that in the REPL, you can recall your last command with Alt+P (for previous) and that tab completion works on most lisp symbols.
[ABORT REQUEST]
.;; An example of getting input from a user and printing it (defun repeat-what-you-say () (let ((what-you-said)) (print "Please say something and type enter:") (setf what-you-said (read-line t)) (print (concatenate 'string "You said: " what-you-said))))
Note that in the REPL, you will see the printed output as well as the return value of the function. The return value is (as you should know by now) the result of the last expression of the function body. Since the PRINT
function returns its argument, the function above also returns it, which is why it is printed twice in the REPL.
'(1 2 3)
) works. '(:keyword "value" :numberfield 123)
You can access the fields of a keyword list with the GETF
function:
CL-USER> (getf '(:a 1 :b 2 :c 3) :b) 2
You can read more about keyword lists in chapter 2 of Practical Common Lisp. For more information about quoting, see this page of the Lisp Primer, and remember that quoting simply prevents a list from being evaluated and instead treats it as data.
'symbol
Symbols are simply short strings, except they are case-insensitive (the REPL always prints them in uppercase) and cannot be changed or manipulated. Like with lists, quoting a symbol simply stops it from being evaluated (i.e. treated as a variable) and instead treats the symbol itself as data.
Write a function that takes a keyword list, looks up the number called :test
and returns the symbol YES
if it is less than 10, and NO
otherwise.
;; Example REPL session showing the concepts you need to use CL-USER> (setf x '(:x 10 :y 33 :test 9)) (:X 10 :Y 33 :TEST 9) CL-USER> (getf x :test) 9 CL-USER> (> (getf x :test) 10) NIL CL-USER> (> 10 (getf x :test)) T CL-USER> (if (> 10 (getf x :test)) 'SMALLER 'BIGGER) SMALLER
agents.lisp
. Save it in a folder for this lab session.,
(comma); this invokes the command mode of the REPL, type the command cd
and press enter. Emacs will prompt you for a file path, enter the file path to the folder where you saved the file.(load “agents.lisp”)
.EXAMPLE-AGENT-PROGRAM
gives an example of how to create a program to control an agent in the vacuum-cleaner world. The program is a function that takes a keyword list representing the percept value, and it should return one of the symbols LEFT, RIGHT, UP, DOWN, SUCK
or IDLE
. A percept keyword list looks like this: '(:x 3 :y 8 :dirty T)
Here, :x
and :y
give the location of the agent and :dirty
tells us if the current square has dirt or not. The given example program is an implementation of the code in figure 2.8 on page 46 in the textbook.
Write a new agent program, that walks about the environment randomly, sucking up dirt where it finds it. You can either write your function in the REPL or add it to the file. If you add it to the file, you have to save the file (keyboard shortcut is Ctrl+X, Ctrl+S) and then reload the file in the REPL (recommended option). Instead of typing the load command again, you can reload the file by hitting Ctrl+C, Ctrl+L while the file window is active.
Use the SIMULATE
and SIMULATE-QUIET
functions from last step to test your new agent program. Does it give better performance evaluation than the old one?
EXAMPLE-MEASURE
shows how performance evaluation functions look. This function takes an environment and an agent as parameters, and returns a number indicating the performance of the agent so far. agents.lisp
with calls to the DEFSTRUCT
macro. This macro defines a function for constructing an instance of the structure, e.g. for the ENV
structure, this function is called MAKE-ENV
. ENV
structure in variable env can be accessed with (env-width env)
.