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

Discussion of Common Lisp
Post Reply
cajetan.bouchard
Posts: 2
Joined: Tue Jun 08, 2010 12:53 pm

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

Post by cajetan.bouchard » Tue Jun 08, 2010 1:02 pm

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
Last edited by cajetan.bouchard on Wed Jun 09, 2010 5:22 am, edited 1 time in total.

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Copy parts of a list into a new list

Post by ramarren » Tue Jun 08, 2010 1:45 pm

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

cajetan.bouchard
Posts: 2
Joined: Tue Jun 08, 2010 12:53 pm

Re: Copy parts of a list into a new list

Post by cajetan.bouchard » Wed Jun 09, 2010 5:22 am

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.

Jasper
Posts: 209
Joined: Fri Oct 10, 2008 8:22 am
Location: Eindhoven, The Netherlands
Contact:

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

Post by Jasper » Wed Jun 09, 2010 7:37 am

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.

Post Reply