kbushnel.sdf-us.org/contact.html
Week 2 answers - before looking in the back of the book relying heavily on
command line GNU Common Lisp compiler for testing answers.
-------------------------------------------
Q - 4.6 Write version of absolute value function MY-ABS using COND instead
of IF.
(defun my-abs (x) (cond ((< x) (abs x)) ((> x) x)))
or
(defun my-abs (x) (cond ((< x 0) (- x)) x)))
-------------------
Q - 4.14 What results do the following expressions produce?
(AND 'FEE 'FIE 'FOE) => FEE #### Student ERROR. The answer is 'FOE
(OR 'FEE 'FIE 'FOE) => FEE
(OR NIL 'FOE NIL) => FOE
(AND 'FEE 'FIE NIL) => NIL
(AND (EQUAL 'ABC 'ABC) 'YES) => T ##### ERROR The answer is 'YES
(OR (EQUAL 'ABC 'ABC) 'YES) => T
I see the error of my ways. AND returns the last clause evaluated if all
are T.
-----------------------------
Q - 4-19
Okay. Let me display my ignorance here. I read the chapter twice and
still don't understand the question, but here goes.
The question read: Show how to write the expression (AND X Y Z W) using
COND instead of AND.
my answer:
(defun whatever (x y z w)
(cond (x)
(y)
(z)
(w)))
Then show how to write it using nested IFs insstead of AND.
my answer:
(defun whatever2 (x y z w)
(cond (if (x) (if (y) (if (z) (if (w) nil))))))
Note: The GNU Common Lisp interpreter didn't show errors while
compiling the function so I'm stickin to it.
------------------------------------
Q - 5.1
(DEFUN GOOD-STYLE (Q P)
(LET ((Q (+ P 5)))
(LIST 'RESULT 'IS Q)))
-------------------------------------------------------------------
Q - 5.2 What is a side effect?
A - A bug.
No really. I've just finished reading the material, and judging by
the example of a side effect, the coin-toss--bug function, a side effect
appears to be an error in programming. In the example the author used
'cond' instead of 'let' to create the 'side effect'. So is there such a
thing as a side effect in lisp? I don't see it, yet.
-----------------------------------------------------------------------
Q 5.6. (I could not find question 5.9 so am working on 5.6)
a)
(DEFUN THROW-DIE() (LIST (+ (RANDOM 6) 1)))
b)
(defun throw-dice ()
(let ((die1 (+ (random 6) 1))
(die2 (+ (random 6) 1)))
(list die1 die2)))
c)
(defun boxcars-p ()
(let ((die1 (+ (random 6) 1))
(die2 (+ (random 6) 1)))
(if (and (equal die1 6) (equal die2 6)) (list t))))
---------------------------------------------------------------------------------
---------------------------------------------------------------------------------
QUESTIONS regarding reading material
So LET* (pg 144) is like list of variables, then? In other languages it
might be int x, y, z.
On page 142 :
(LET ((VAR-1 VALUE-1)
(VAR-2 VALUE-2)
. . .
(VAR-N VALUE-N))
BODY)
So what's the difference between LET and LET*?
-------------------------------------------------------------------------------
Blog fodder:
Where I'm at. I tried to install CLISP on FreeBSD. Didn't work so
installed GNU Common Lisp MS Windows version. There's an emac for windows
also, I believe, but I prefer Notepad++ (which offers lisp formatting)
calling files from the GNU command line when using Windows. Will use Emacs
on FreeBSD when I get it working.
I'm highly motivated to use FreeBSD for portability of the operating
system environment. For now the MS Windows version allows for testing of
LISP functions.
My goal is not mastery of LISP, but an overview.