Some element-deleting list functions

Discussion of Common Lisp
drosales
Posts: 6
Joined: Tue Oct 04, 2011 11:53 am

Some element-deleting list functions

Post by drosales » Sat Oct 08, 2011 10:26 am

I am trying to write a function that takes two inputs X and L, the second one of which is a list, the first one may or may not be a list.
The function will then search L and take out THE FIRST APPEARANCE of X in L, and will return the rest of L untouched.
I wrote what I thought (after a series of revisions) was a correct solution to this simple problem:

Code: Select all

(defun takeoutFirst(X L)
	(cond
		((null L) nil)
		((null X) L)
		((equal X (car L)) (cdr L))
		(t (append (car L) (takeoutFirst X (cdr L))))))
The compiler's (GLC 2.6.6) only output is "Broken at IF".

smithzv
Posts: 94
Joined: Wed Jul 23, 2008 11:36 am

Re: Some element-deleting list functions

Post by smithzv » Sat Oct 08, 2011 11:31 am

Is this homework? I ask because there is the REMOVE function... (edit: yes, clearly I don't know what REMOVE does... :oops: )

Your code is almost correct, I think. However, I don't know why you are checking for (null x), and that last case should be (cons (car l) (take-out-first x (cdr l))) not APPEND. What does "X may or may not be a list" mean in terms of how the function works?

That GCL error message is not very helpful, is it?

drosales
Posts: 6
Joined: Tue Oct 04, 2011 11:53 am

Re: Some element-deleting list functions

Post by drosales » Sun Oct 09, 2011 1:44 am

There is a remove function, correct, but as far as I understand REMOVE will remove all occurrences of the object with the value you described, right?
I need to remove only THE FIRST occurrence of X.
I stated that X may be a list(actually i checked again and it is a cons structure.. for sure), and so I check for null X (I guess I may as well skip that line), but this has no effect over the compiler message, I already compiled
without this line, the result is the same.
YES, this is homework (but im not cheating! I wrote this but the T.A. is as helpful is the gcl compiler or less with those comments.).
The homework problem is this exactly:
"
Define a function takeoutFirst ( X L ) which takes out the first occurrence of the cons
structure X from a list L.
For exà̃˿e: ( takeoutFirst ‘(a b) ‘( ( b b) (a b) ( b a) (a b) (c c) ))
( ( b b) ( b a) (a b) (c c) )
"
.
Now, thinking that maybe I was having a problem using the COND macro, I reorganized the code into an if-else code:

Code: Select all

(defun takeoutFirst2(X L)
	(if (null L)
		nil
		(if(equal X (car L)) 
			(cdr L)
			(append (car L) 
				(takeoutFirst X (cdr L))))))
But this was to no avail... compiler's message was exactly the same; no more, no less.

smithzv
Posts: 94
Joined: Wed Jul 23, 2008 11:36 am

Re: Some element-deleting list functions

Post by smithzv » Sun Oct 09, 2011 2:14 am

Regarding REMOVE, I tried to change it before it caused confusion. Just me being stupid, sorry.

I actually don't have any hints. My first suspicion is that your GCL install is screwed up. I just tried it in SBCL and it worked like a charm. I'm downloading GCL to see what's up there right now.

Sorry GCL sucks right now. Very few CL programmers use it as it has been really left in the dust by a handful of other FOSS options over the last ten (more?) years. You might be able to get more information from GCL with the right commands. For instance, you can usually get a backtrace of the call stack or inspect local variables. Refer to the GCL manual for help.

smithzv
Posts: 94
Joined: Wed Jul 23, 2008 11:36 am

Re: Some element-deleting list functions

Post by smithzv » Sun Oct 09, 2011 2:21 am

Yeah, I just installed GCL and ran your code and no errors. You will almost certainly need to reinstall GCL. Did you build from source, or download a binary? (I always say go binary if you can). If you are reinstalling your Lisp system, you might as well go with some other Lisp unless your class said GCL is the best one for the class. A CLISP install is pretty hard to screw up and is stable just about everywhere.

drosales
Posts: 6
Joined: Tue Oct 04, 2011 11:53 am

Re: Some element-deleting list functions

Post by drosales » Sun Oct 09, 2011 5:29 am

Thank you very much for your help, Mr. Smith. I changed the "append" to "cons" and it just worked...

wvxvw
Posts: 127
Joined: Sat Mar 26, 2011 6:23 am

Re: Some element-deleting list functions

Post by wvxvw » Sun Oct 09, 2011 6:35 am

Just because of my curiosity. I thought what if the requirement was to modify the input list. Well, it's not possible to tell from the description. So, I tried to see what I could get, if that would be the case, but I'm not really liking what I have:

Code: Select all

(defun take-out-first (element list)
  (do ((current list (cdr current))
       (previous))
      ((or (null current) (not (listp current))))
    (when (equal element (car current))
      (if previous
	  (rplacd previous (cdr current))
	  (pop list))
      (return))
    (setf previous current))
  list)
Would there be a better way to do that?

drosales
Posts: 6
Joined: Tue Oct 04, 2011 11:53 am

Re: Some element-deleting list functions

Post by drosales » Sun Oct 09, 2011 8:33 am

At first I also thought I had to modify the list. Then I saw there was no such requirement, just to return the resulting list after the operation is applied.
I tried to modify the list received as parameter before,too, with setf, but I failed repeatedly. I'll try to look in to what you've written too.

In the meantime I'll post another problem I am facing right now. I am using my previous function takeoutFirst to implement takeoutSecond, by iterating inside takeoutSecond until i find the first occurrence of the target object, and then sending the list's cdr to takeoutFirst, yet I get a compiler run-time error.

Code: Select all

(defun takeoutSecond(X L)
	(cond
		((null L) nil)
		((equal X (car L))
			(cons (car L) (takeoutFirst X (cdr L))))
		(t
			(takeoutSecond X (cdr L)))))
and i got some strange error about the recurssion, I cannot tell you cause I cannot remember and now that I tried to recompile it to see the error again, it gives me a compiler error about "_" being an unbound variable @@.
Any help would be appreciated.

nikond3s
Posts: 9
Joined: Mon Oct 03, 2011 10:05 am

Re: Some element-deleting list functions

Post by nikond3s » Sun Oct 09, 2011 9:26 am

If takeoutSecond has to remove the second match, why wouldn't you do something like the following?

Code: Select all

(defun takeoutSecond(X L)
  (takeOutFirst X (takeOutFirst X L)))
EDIT: Ah! You just want do delete the second while leaving the first one untouched. Well than forget my writings, I was wrong. ;)

wvxvw
Posts: 127
Joined: Sat Mar 26, 2011 6:23 am

Re: Some element-deleting list functions

Post by wvxvw » Sun Oct 09, 2011 9:36 am

Wait, what is a compiler runtime error? Usually you call a compile time error an error that happened because the compiler could not understand your program. Example of a compile time error - mismatched parens. Run time error is when the compiler was able to digest the program text, but the program itself was erroneous. Example - you called a multiply function with an argument which is not a number.
But if that code gives you that kind of error (I'd imagine it's a compile time error), the compiler is doing something very odd. Try reinstalling or using a different version. Another guess... is your system using any locale other then English? Is it possible that while typing the program you used some Unicode characters or "high ASCII" (that is characters with codes > 128)? Because it totally looks like there is some syntax problem, where the program is interpreted entirely different from what was intended.

Post Reply