Jump to content

Featured Replies

Posted

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?

 

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

 

  • Author

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

it's very lispian 😄  yes, it works also by (mapcar...)

  • Author

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.

 

Create an account or sign in to comment


Copyright © 2014-2025 Opusmodus™ Ltd. All rights reserved.
Product features, specifications, system requirements and availability are subject to change without notice.
Opusmodus, the Opusmodus logo, and other Opusmodus trademarks are either registered trademarks or trademarks of Opusmodus Ltd.
All other trademarks contained herein are the property of their respective owners.

Powered by Invision Community

Important Information

Terms of Use Privacy Policy