Page 1 of 1

Copy parts of a list into a new list [solved]

Posted: Tue Jun 08, 2010 1:02 pm
by cajetan.bouchard
Hi everyone,

I'm new on this forum so I hope this question haven't been ask yet. I'm also new to lisp / common lisp this might be a pretty easy question.

I manage to copy an "old-list" into a "new-list". But what I would like to do is to take out some item of the lisp.
For example, if I have
(defparameter *foo* (list "hello" "my" "name" "is "Ethan"))
I would like to take out every work that has an "e" inside it so that my new list would look like ("my" "is").

This is the code that I have to copy to a new list

Code: Select all

(defparameter *big-x* (list (list 1 2 3) (list 4 5 6)))

(defun extract-my-history2 ()
              (let (new-list)
                (dolist (frame *big-x*)
                  (setf new-list (append new-list (list frame))))
                new-list))

(setf b (extract-my-history2))
Thank you in advance for your help
Cajetan

Re: Copy parts of a list into a new list

Posted: Tue Jun 08, 2010 1:45 pm
by ramarren
cajetan.bouchard wrote:I would like to take out every work that has an "e" inside it so that my new list would look like ("my" "is").
This is best achieved using a standard REMOVE-IF function, combined with FIND. Sequence manipulation functions are enumerated in the sequences chapters in the Hyperspec.

Code: Select all

(remove-if (lambda (string)
             (find #\e string :test #'char-equal))
           *foo*)

Re: Copy parts of a list into a new list

Posted: Wed Jun 09, 2010 5:22 am
by cajetan.bouchard
Thank you for the reply, I'm still amaze how the code is short in lisp. Also the 3 link for documentation are really useful.

Re: Copy parts of a list into a new list [solved]

Posted: Wed Jun 09, 2010 7:37 am
by Jasper
Actually, i'd use alexandria:

Code: Select all

(remove-if (alexandria:curry #'find #\e) *foo*)
(currying is useful when the equivalent lambda form very simple.)Note that the default :test, #'eql works too, but there might be some performance advantage in using char=, unfortunately type declarations don't seem powerful enough on lists. Nothing like (declaim (type (list-of string) *foo*)) afaik.

Incase you haven't found it yet, there is lispdoc.com for searching documentation, i often use C-f on this symbol list, and cliki for libs. Feel free to ask questions if the answer isn't readily found, though.