nila_joy Posted November 13, 2011 Posted November 13, 2011 Hi, just joined cadtutor. Its already helped me a lot. thnx to you all guys. I am a beginner in Lisp , I want to manipulate a list. ((1.y) (2.b) (3.d) (4.f)) , this is a list. 1. I want this as (1.b) , (2.y) like this. 2. I want this as (1 2 3 4 ) (y b d f) I know u all are experts and this post will make u all lough, but from last 2 days I am trying to do this, don't know which function should I use . Quote
pBe Posted November 13, 2011 Posted November 13, 2011 (edited) Is that (1.y) or (1 . y)? Anyhooo.. if it is (defun splt (lst / num alp) (foreach itm (reverse lst) (setq num (cons (car itm) num) alp (cons (cdr itm) alp) ) ) (list num alp) ) (defun splt (lst / a b num alp) (setq lst (reverse lst)) (while (setq a (car lst)) (setq b (cdr lst) num (cons (car a) num) alp (cons (cdr a) alp) ) (setq lst b) ) (list num alp) ) Edited November 13, 2011 by pBe Quote
David Bethel Posted November 13, 2011 Posted November 13, 2011 For #2 you could use (mapcar) [b][color=BLACK]([/color][/b]setq lst '[b][color=FUCHSIA]([/color][/b][b][color=NAVY]([/color][/b]1 . y[b][color=NAVY])[/color][/b][b][color=NAVY]([/color][/b]2 . b[b][color=NAVY])[/color][/b][b][color=NAVY]([/color][/b]3 . d[b][color=NAVY])[/color][/b][b][color=NAVY]([/color][/b]4 . f[b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b] [b][color=BLACK]([/color][/b]setq l1 [b][color=FUCHSIA]([/color][/b]mapcar 'car lst[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b] [b][color=BLACK]([/color][/b]setq l2 [b][color=FUCHSIA]([/color][/b]mapcar 'cdr lst[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b] There are lots of ways to do this. Good Luck -David Quote
Tharwat Posted November 13, 2011 Posted November 13, 2011 My version and it is not that far than David's codes .. (setq lst '((1 . y) (2 . b) (3 . d) (4 . f))) (mapcar (function (lambda (x) (setq x1 (cons (car x) x1)) (setq x2 (cons (cdr x) x2)) ) ) lst ) (reverse x1) (reverse x2) Quote
Lee Mac Posted November 13, 2011 Posted November 13, 2011 Another: (setq lst '((1 . y) (2 . b) (3 . d) (4 . f))) (apply 'mapcar (cons 'list (mapcar '(lambda ( x ) (list (car x) (cdr x))) lst))) Quote
Tharwat Posted November 13, 2011 Posted November 13, 2011 Another .. (setq lst '((1 . y) (2 . b) (3 . d) (4 . f))) (defun Picks (lst / i j) (foreach x (reverse lst) (setq i (cons (car x) i)) (setq j (cons (cdr x) j)) ) (princ i) (princ j) (princ) ) (Picks lst) Quote
nila_joy Posted November 13, 2011 Author Posted November 13, 2011 Thnx Tharwat, David and PBE... I got 2 functions, mapcar and lambda...and I got my ans. Thnx again guys. Thanks from India. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.