Re: newbie exploring better ways



On 30 Jan, 13:59, Mark Tarver <dr.mtar...@xxxxxxxxxxxxxx> wrote:
On 30 Jan, 03:42, vijay <vijay...@xxxxxxxxx> wrote:

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)))

In Qi

(define remv
  _ [] -> []               \second input = [] (NIL)? then return [] \
  X [X | Y] -> Y        \first input = head of second input, return
tail of second input\
  X [_ | Y] -> (remv X Y)) \otherwise recurse on the tail of the
second input\

I generally give the Lisp equivalent to any Qi solution, but since you
mention 'the dreaded 'h word' I suppose I'd better not.  You should be
able to figure out the Lisp.

Mark

Darn - forgot to cons the head - should be

(define rem
\second input = [] (NIL)? then return [] \
_ [] -> []
\first input = head of second input return tail of second input\
X [X | Y] -> Y
\otherwise cons the head and recurse on the tail of the second input
\
X [Y | Z] -> [Y | (rem X Z)])

Mark
.