Re: newbie exploring better ways
- From: Kent M Pitman <pitman@xxxxxxxxxxx>
- Date: 30 Jan 2008 01:39:36 -0500
vijay <vijaykcm@xxxxxxxxx> writes:
The following function removes only the first occurrence of an element
from a list. Is this very different than how experienced Lispers do
things? The function is part of my homework assignment. However, my
homework is just to write a function that does its job. I am trying to
learn if there are better ways to do it.
(defun remv (item l)
"a function to remove (without modifying the list) the FIRST
occurrence of a given element"
(let ((result-list nil)(flag 0))
(dolist (curr l)
(if (equalp curr item)
( if(equalp flag 1) (push curr result-list) (setq flag 1))
(push curr result-list)))
(reverse result-list)))
"nil)(flag" => "nil) (flag"
Never use ")(" without space between.
"( if" => "(if "
Try never to use an open paren followed by a symbol with horizontal
whitespace in between unless there is a strong special reason. (You
don't have such a reason here.)
"if(" => "if ("
A symbol folowed by an open-paren should always have whitespace between.
Instead of 0/1, why not use T/NIL values?
Did your teacher suggest EQUALP for the comparison of curr with item?
EQUALP is not the normal predicate Lisp uses.
Why are you using EQUALP rather than EQL for comparing to 1? EQUALP
is a "forgiving" compare that tends to be slow. In this case, a
compiler may optimize the use. But try to use EQL in cases where it
works. (If you rewrite without the 0/1 thing, this use may go away of
its own accord.
When talking about an empty list, use '() rather than nil as a matter
of style. They are the selfsame object, but notationally nil as a
variable usually cues the human programmer reading your code that
false is intended, while '() cues the human that it's an empty list.
remv is a terrible function name. If the teacher gave it to you, tell
him/her I suggested remove1 as a better name. Lisp isn't just a unix
shell scripting language. Vowels are ok.
Likewise, I encourage variable names spelled out in most cases. current
rather than curr, for example.
If I kept the text as you'd written it, I'd still rather nreverse at
the end than reverse. There's no reason to cons up new structure when
you've already just made a bunch of structure and are about to throw
it away. But ...
Extra credit: Rather than uselessly looping down the list after you find
the thing you want, why not learn about NRECONC. There are so few examples
of places you can use that function. And this is one of them.
Btw, since you asked: Real Lisp programmers would just call REMOVE
with appropriate args.
(remove 3 (list 1 2 3 1 2 3 1 2 3) :count 1)
=> (1 2 1 2 3 1 2 3)
.
- References:
- newbie exploring better ways
- From: vijay
- newbie exploring better ways
- Prev by Date: Re: Announcing a new release of Lisp1 #5
- Next by Date: Re: newbie exploring better ways
- Previous by thread: Re: newbie exploring better ways
- Next by thread: Re: newbie exploring better ways
- Index(es):
Relevant Pages
|
|