INPUT BUFFERED FILE-STREAM CHARACTER

Discussion of Common Lisp
Post Reply
notalisper
Posts: 1
Joined: Mon Apr 11, 2011 10:48 am

INPUT BUFFERED FILE-STREAM CHARACTER

Post by notalisper » Mon Apr 11, 2011 12:15 pm

While writing a function involving dolist i received an error "should be a lambda expression" with the following code:

Code: Select all

(defun removeSortIterative(x l)
	(let ((result 0))
		(dolist (e l result) (if (equals x e) (setq result (result)) (setq result ((append (list e) result)))))))
I did some research and it seemed as though the error was due to the double parentheses as an open parentheses indicates a function and (append (list e) result) is not a function. Therefore I added a function so my code now reads:

Code: Select all

(defun removeSortIterative(x l)
	(let ((result 0))
		(dolist (e l result) (if (equals x e) (setq result (result)) (setq result (append (append (list e) result) nil)))))))
I am now, however, receiving an error that reads "input buffered file-stream character". I would appreciate any help on solving this debacle. I am new to lisp and cannot seem to figure out what's going wrong.

Thanks!
Last edited by nuntius on Mon Apr 11, 2011 6:53 pm, edited 1 time in total.
Reason: add code tag

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

Re: INPUT BUFFERED FILE-STREAM CHARACTER

Post by nuntius » Mon Apr 11, 2011 6:56 pm

What does (result) do? (not a standard CL function)

Note its also common to (setq result (if test a b))

vanekl
Posts: 12
Joined: Wed Dec 15, 2010 10:25 am

Re: INPUT BUFFERED FILE-STREAM CHARACTER

Post by vanekl » Tue Apr 12, 2011 9:27 am

Here are three alternatives.

Code: Select all

(defun removeSortIterative(x l)
  (let ((result ()))
    (dolist (e l result)
      (when (not (equal x e))
	(setq result (append result (list e)))))))

(defun removeSortIterative(x list)
  (reduce (lambda (acc e)
	    (when (not (equal x e))
	      (nconc acc (list e))))
	  list :initial-value (list 0)))

(defun removeSortIterative (x list)
  (let ((result ()))
    (map 'nil (lambda (e)
		(when (not (equal x e))
		  (push e result)))
	 list)
    (reverse result)))

Post Reply