list initialization in a function

Discussion of Common Lisp
Post Reply
prathyuguduri
Posts: 12
Joined: Tue Apr 20, 2010 11:12 pm

list initialization in a function

Post by prathyuguduri » Tue Apr 20, 2010 11:23 pm

I have written a function below and I'm calling it through another function.

Code: Select all

(defun RTP (KB)
(

  setq sos (last KB)
  setq sos (first sos)
  setq soslist (list sos)
  setq clauses (reverse KB)
  setq pred1 (first (rest clauses))


(if (and (equal (length sos) 2)
         (member (second sos) pred1)) 
(setq output (remove (second sos) pred1)
and setq output (rest output)
and setq soslist (append (list output) soslist)))

setq sos output
setq clauses (rest clauses)
setq pred1 (first (rest clauses))

(if (and (equal (length sos) 1)
         (member (first sos) pred1)
		 (equal(nth notpos pred1) 'not)) 
(setq output (remove-at pred1 (+ notpos 1))
and setq output (remove (first sos) output)
and setq output (rest output)
and setq soslist (append (list output) soslist)))

)
)
Error: Cannot setq (FIRST SOS) -- not a symbol.
Only the first statement is getting executed and the error is displayed. Should n't the setq statements be written like that? Could someone tell me what is wrong

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: list initialization in a function

Post by ramarren » Tue Apr 20, 2010 11:34 pm

The code you posted doesn't seem to be valid Lisp, and certainly not valid Common Lisp. Please read a book like Practical Common Lisp if you want to learn Common Lisp.

prathyuguduri
Posts: 12
Joined: Tue Apr 20, 2010 11:12 pm

Re: list initialization in a function

Post by prathyuguduri » Tue Apr 20, 2010 11:46 pm

You mean the whole function, even the if loop. Because when I execute these statements separately in the lisp interpreter they do execute. I'm having problem only with the first setq statements

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: list initialization in a function

Post by nuntius » Wed Apr 21, 2010 1:44 am

0) You are missing parentheses all over. CL looks like (setq sos (last KB)). Yet there's a stray paren near the top of your function.
1) Setq needs variables; I don't see declarations for sos, soslist, clauses, etc.
2) There is no such thing as an "if loop".

Here's how CL sees your function

Code: Select all

(defun RTP (KB)
  (setq sos (last KB)
        setq sos
        (first sos) setq
        soslist (list sos)
	...))
You might attempt something like

Code: Select all

(defun RTP (KB)
  (let* ((sos (first (last KB)))
         (soslist (list sos))
         (clauses (reverse KB))
         (pred1 (first (rest clauses))))
    ...))

prathyuguduri
Posts: 12
Joined: Tue Apr 20, 2010 11:12 pm

Re: list initialization in a function

Post by prathyuguduri » Wed Apr 21, 2010 6:13 pm

@nuntius

Thank you for the reply. I have replaced if with when. And the mistake I was doing is that I was trying to see the value of the variable in the interpreter after the function got executed. The variable scope is local to the function so I was getting the error that the list sos is unbound.

Thanks again.

Post Reply