From: ······@smartchat.net.au
Subject: AI book by Peter Norvig
Date: 
Message-ID: <e7jfk2p967mpn70849pqe3nugjii26pgk4@4ax.com>
hi  all   
I've just finished reading chapter 2 of  Paradigms of ai programming
by norvig but i can't get his generate program to work.

i'm using cormans lisp on a mac and i've checked and rechecked
everything but still no luck.

when i type (generate 'sentence) i get (sentence) instead of for
example (the girl hit the ball). if i type (generate 'verb-phrase)
i get (verb-phrase).  obviusly something is drastically wrong, but i
can't see what.

please guys i need help with this because otherwise i can't proceed
with the book.
thanks.

From: mark main
Subject: Re: AI book by Peter Norvig
Date: 
Message-ID: <1162337061.137405.239190@k70g2000cwa.googlegroups.com>
I was having the same problem when I did the exercise. I don't remember
what I did to solve the issue, but I do know that you need to go over
the source again. Sorry...
From: mark main
Subject: Re: AI book by Peter Norvig
Date: 
Message-ID: <1162337064.555050.275170@b28g2000cwb.googlegroups.com>
I was having the same problem when I did the exercise. I don't remember
what I did to solve the issue, but I do know that you need to go over
the source again. Sorry...
From: Victor Kryukov
Subject: Re: AI book by Peter Norvig
Date: 
Message-ID: <m23b9482zw.fsf@wireless-207-244.uchicago.edu>
I guess it would be helpful for the group if you post the code that
you're trying to run. Otherwise people who don't have a book with them
could not help[*].

Interestingly enough, I've just got the same book today from the
library. I've decided not to ask any more questions here until I at
least master Graham's "ANSI Common LIsp" and "On Lisp", Norvig's
"Paradigms" and PCL :).

Norvig's book is a wonderful reading - it's probably the best book I've read on
programming so far. I haven't finished chapter 2 yet (as I was reading
some more advanced chapters - I just couldn't resist :), but as I read
it, I'll try to help you.

Cheers,
Victor.

[*] However, some people in this group do have a paranormal ability to read
what's going on in newbies minds...
From: Pedro Kröger
Subject: Re: AI book by Peter Norvig
Date: 
Message-ID: <87velzww72.fsf@gmail.com>
······@smartchat.net.au writes:

> hi  all   
> I've just finished reading chapter 2 of  Paradigms of ai programming
> by norvig but i can't get his generate program to work.

the code from the version that I have runs just fine. have you checked
the errata?

http://www.norvig.com/paip-errata.html

you may want to check the code that is online to see if it's different
from the code you have:

http://www.norvig.com/paip/simple.lisp

Pedro Kroger
From: Pascal Bourguignon
Subject: Re: AI book by Peter Norvig
Date: 
Message-ID: <87iri0f1d3.fsf@thalassa.informatimago.com>
······@smartchat.net.au writes:

> hi  all   
> I've just finished reading chapter 2 of  Paradigms of ai programming
> by norvig but i can't get his generate program to work.
>
> i'm using cormans lisp on a mac and i've checked and rechecked
> everything but still no luck.
>
> when i type (generate 'sentence) i get (sentence) instead of for
> example (the girl hit the ball). if i type (generate 'verb-phrase)
> i get (verb-phrase).  obviusly something is drastically wrong, but i
> can't see what.

Perhaps your *grammar* is not set ?

(setf *grammar*  *bigger-grammar*)

> please guys i need help with this because otherwise i can't proceed
> with the book.
> thanks.


CL-USER> (defpackage "COM.NORVIG"
            (:use "COMMON-LISP")
            (:SHADOW "EXP"))
#<PACKAGE COM.NORVIG>
CL-USER> (in-package "COM.NORVIG")
#<PACKAGE COM.NORVIG>
COM.NORVIG> (LOAD "NORVIG:INTRO.LISP") ;; A few simple definitions
;; Loading file /home/pjb/src/lisp/norvig/intro.lisp ...
;; Loaded file /home/pjb/src/lisp/norvig/intro.lisp
T
COM.NORVIG> (LOAD "NORVIG:SIMPLE.LISP") ;; Random sentence generator (two versions)
;; Loading file /home/pjb/src/lisp/norvig/simple.lisp ...
;; Loaded file /home/pjb/src/lisp/norvig/simple.lisp
T
COM.NORVIG> (loop repeat 10 do (print (generate 'sentence)))

(ROBIN LIKED SHE) 
(SHE HIT A WOMAN IN HE) 
(THE MAN LIKED A WOMAN WITH THAT IN THE WOMAN WITH A ADIABATIC BALL IN SHE) 
(A TABLE IN IT ON THE TABLE WITH THOSE LIKED PAT) 
(TERRY SAW THE ADIABATIC GREEN MAN TO ROBIN TO ROBIN TO HE) 
(LEE LIKED KIM ON PAT BY THESE TO PAT) 
(THE GREEN MAN BY PAT SAW A BLUE TABLE BY LEE TO TERRY IN THE BALL BY KIM IN
 TERRY IN IT WITH A LITTLE LITTLE TABLE) 
(THE WOMAN SAW KIM) 
(THOSE HIT THE LITTLE ADIABATIC WOMAN) 
(SHE TOOK KIM TO THESE ON ROBIN ON TERRY IN A ADIABATIC WOMAN ON IT BY A BALL) 
NIL

;; The other version is:

COM.NORVIG> (sentence)
(THE GREEN WOMAN HIT THE ADIABATIC BLUE TABLE ON A TABLE)


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

"Our users will know fear and cower before our software! Ship it!
Ship it and let them flee like the dogs they are!"
From: Sidney Markowitz
Subject: Re: AI book by Peter Norvig
Date: 
Message-ID: <4547e337$0$88629$742ec2ed@news.sonic.net>
······@smartchat.net.au wrote, On 1/11/06 11:35 AM:
> when i type (generate 'sentence) i get (sentence) instead of for
> example (the girl hit the ball). if i type (generate 'verb-phrase)
> i get (verb-phrase).  obviusly something is drastically wrong, but i
> can't see what.

This will be a good exercise in understanding how it works. If you look
at the source code, you will see that the generate function is a cond
with three parts, that the first one is not applicable in the case of
(generate 'sentence) and that the last part evaluates to '(sentence)
which is what you are seeing. So you need to figure out why the test
portion of the second part of the cond is evaluating to nil.

That second part evaluates (rewrite 'sentence) which is defined as
evaluating (rule-rhs (assoc 'sentence *grammar*))

(rule-rhs rule) is defined as (rest (rest rule)), in other words it just
pops off the first two elements of its list argument. So you just need
to look at what is going wrong with (assoc 'sentence *grammar*) which
means seeing what is in the global variable *grammar*. If what is in
there is not correct, look in the source code to find out how it's value
is set. You can see that it is done with a (defvar *grammar*
*simple-grammar*) and that *simple-grammar* is initialized using a
defparameter. If *grammar* is wrong but *simple-grammar* is correct,
that is an indication that somehow *grammar* was defined before the
defvar, since defvar only binds a value if the variable is unbound at
the time, it does not re-initialize a variable.

I simplified the analysis a little bit by assuming that the second part
of the cond could never evaluate to '(sentence). To confirm that, try to
follow what is supposed to happen when you evaluate (generate
'sentence). In the second part of the cond, (assoc 'sentence *grammar*)
is supposed to evaluate to '(SENTENCE -> (NOUN-PHRASE VERB-PHRASE)), the
first two elements are popped off, and a random element of the result is
selected, in this case always '(noun-phrase verb-phrase). Then generate
is recursively called on each of the elements in that list. If you get
that far, there is no way for generate to return '(sentence) as the
symbol 'sentence is no longer anywhere in the input.

So see what you have in *grammar* after you load the examples, and see
what you have in *simple-grammar* and track down the problem from there.

-- 
    Sidney Markowitz
    http://www.sidney.com