Jump to content

append item to all sublists


erka

Recommended Posts

Hi,

I have this list of lists (will have much more sublists) :

'((s -s s== -s -s== s== -s s -s s -s s -s==) (s== -s s -s s== -s s -s== s== -s s -s s -s==) (s -s== s -s s -s s== -s s -s==))

I want to append e.g. a 'q to the end of each list.

Is there a function I could use?

How would you do this?

 

Link to comment
Share on other sites

like that?

 

;; as lisp-code ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(setf alist '((s -s s== -s -s== s== -s s -s s -s s -s==) (s== -s s -s s== -s s -s== s== -s s -s s -s==) (s -s== s -s s -s s== -s s -s==)))

(loop for i in alist
      collect (append i (list 'q)))

=> ((s -s s== -s -s== s== -s s -s s -s s -s== q) (s== -s s -s s== -s s -s== s== -s s -s s -s== q) (s -s== s -s s -s s== -s s -s== q))


;; as lisp-function ;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun append-value (lists value)
  (loop for i in lists
      collect (append i (list value))))

(append-value alist 'q)

=> ((s -s s== -s -s== s== -s s -s s -s s -s== q) (s== -s s -s s== -s s -s== s== -s s -s s -s== q) (s -s== s -s s -s s== -s s -s== q))

 

Link to comment
Share on other sites

Thank you AM.

That would work.

 

I found this in the opusmodus functions. 

(setf alist '((s -s s== -s -s== s== -s s -s s -s s -s==) (s== -s s -s s== -s s -s== s== -s s -s s -s==) (s -s== s -s s -s s== -s s -s==)))

 

(mapcar #'(lambda (x) (append x '(q) )) alist).  

=> ((s -s s== -s -s== s== -s s -s s -s s -s== q) (s== -s s -s s== -s s -s== s== -s s -s s -s== q) (s -s== s -s s -s s== -s s -s== q))

Link to comment
Share on other sites

Yes, looks complicated. I used the loop advice now. Easier to understand. 

I was looking for a function without a loop, but then you have to use a lambda expression.

To get into the loop-macro with all its possibilities is still on my list. But the collect does all I need.

I get the idea that learning the loop-macro features is worth the effort.

 

Thanks again.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

Terms of Use Privacy Policy