Jump to content

Recommended Posts

Posted

Exactly this is how i need List1 = ((0 0) (1 0.1) (2 0.1) (3 0.1) (4 0.2) (5 0.2) (6 0.3) (7 0.3))First of all the list given above can vary every time secondly i want to separate the list for same cdr elements and store them to a variable Like i want like this

List A = (0 0)

List B = (1 0.1) (2 0.1)(3 0.1)

List C = ............................etc

Thanks

 

 

OR

 

 

 

if i get a list like this is also good

list1 ((0 0)(1 .1)(4 .2)(6 .3))

Posted

Consider the following function:

 

[color=GREEN];; Group By Function  -  Lee Mac[/color]
[color=GREEN];; Groups items considered equal by a given predicate function[/color]

([color=BLUE]defun[/color] LM:GroupByFunction ( lst fun [color=BLUE]/[/color] tmp1 tmp2 x1 )
   ([color=BLUE]if[/color] ([color=BLUE]setq[/color] x1 ([color=BLUE]car[/color] lst))
       ([color=BLUE]progn[/color]
           ([color=BLUE]foreach[/color] x2 ([color=BLUE]cdr[/color] lst)
               ([color=BLUE]if[/color] (fun x1 x2)
                   ([color=BLUE]setq[/color] tmp1 ([color=BLUE]cons[/color] x2 tmp1))
                   ([color=BLUE]setq[/color] tmp2 ([color=BLUE]cons[/color] x2 tmp2))
               )
           )
           ([color=BLUE]cons[/color] ([color=BLUE]cons[/color] x1 ([color=BLUE]reverse[/color] tmp1)) (LM:GroupByFunction ([color=BLUE]reverse[/color] tmp2) fun))
       )
   )
)

 

Call the above with an appropriate predicate function:

 

(LM:GroupByFunction
  '(
       (0 0)
       (1 0.1)
       (2 0.1)
       (3 0.1)
       (4 0.2)
       (5 0.2)
       (6 0.3)
       (7 0.3)
   )
   (lambda ( a b ) (= (cadr a) (cadr b)))
)

Returns:
(
   ((0 0)) 
   ((1 0.1) (2 0.1) (3 0.1))
   ((4 0.2) (5 0.2))
   ((6 0.3) (7 0.3))
)

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