Jump to content

Recommended Posts

Posted

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 . :cry:

Posted (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 by pBe
Posted

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

Posted

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)

Posted

Another:

 

(setq lst '((1 . y) (2 . b) (3 . d) (4 . f)))

(apply 'mapcar (cons 'list (mapcar '(lambda ( x ) (list (car x) (cdr x))) lst)))

Posted

Another .. :D

 

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

Posted

Thnx Tharwat, David and PBE... I got 2 functions, mapcar and lambda...and I got my ans.

 

Thnx again guys. Thanks from India.

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

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