This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Some element-deleting list functions

14 posts · 11994 views

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:
(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".

Re: Some element-deleting list functions

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?

Re: Some element-deleting list functions

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:
(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.

Re: Some element-deleting list functions

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.

Re: Some element-deleting list functions

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.

Re: Some element-deleting list functions

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

Re: Some element-deleting list functions

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:
(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?

Re: Some element-deleting list functions

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.
(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.

Re: Some element-deleting list functions

If takeoutSecond has to remove the second match, why wouldn't you do something like the following?
(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. ;)

Re: Some element-deleting list functions

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.

Re: Some element-deleting list functions

Ok, I'm gonna try the (possible) easy solution: compiler's problem.
What compilers/environments/whatever_you_call_it do you most recommend?
My system is Windows 7 with language selection turned to English, yet it is originally set in Mandarin Chinese.

Re: Some element-deleting list functions

run-time: happens when you run the program, after it has been compiled.
compile-time: during compiling :) the program is not compiled because an error occurred while attempting to do it

Re: Some element-deleting list functions

I don't have any experience with commercial Lisps, and all of my experience is roughly limited to SBCL and CLISP. They both worked for me under Windows 7 and Linux, different versions. I don't have enough experience to judge about which is better, however it seems that SBCL has more users / it's easier to find help on SBCL-specific issues then other Lisps, but maybe it's just an impression I've got. I think I followed this article http://www.pchristensen.com/blog/articl ... indows-xp/ when I needed to set up my Windows machine (although, pay attention that download links in the top of the article will be dated, you'll need the same programs, but new versions).

[OT] Lisp-"Environment"

For Windows I arrived at http://lispcabinet.sourceforge.net/. It's some kind of "fire-and-forget"-installer. It comes with Emacs and during the install-routine you can choose to install additional Lisps, no other configuration required. I run it under Windows 7 64bit.