TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: whdjr on June 08, 2005, 05:06:31 PM

Title: Deconstructing Lists
Post by: whdjr on June 08, 2005, 05:06:31 PM
Is there a lisp command that will turn this
Code: [Select]
;;;One single list
((1 2 3)(4 5 6))
into this
Code: [Select]
;;;Two seperate lists
(1 2 3)(4 5 6)
or do you just have to use car and cadr?
Title: Deconstructing Lists
Post by: Kerry on June 08, 2005, 05:19:28 PM
Not sure what you may be trying to do with the discombobulation Will, but I can not think of an easier way than car, cadr.
What is the construct you are using this in ? .. It seems unusual to have 2 consecutive lists without a container .
Title: Deconstructing Lists
Post by: whdjr on June 08, 2005, 05:51:34 PM
I have a mapcar that returns two lists of strings.  I have another piece of code that returns the common strings from the two lists.  This code takes two arguments, the two lists.  I could change the code to accept a list, but I didn't want to have to change it as I use it for other functions as well.
Title: Deconstructing Lists
Post by: MP on June 08, 2005, 06:03:27 PM
Sample data and desired output please.

:)
Title: Deconstructing Lists
Post by: JohnK on June 09, 2005, 08:45:51 AM
I would also like to see some example data; I have a few string and list functions that, maybe, could be of use.
Title: Deconstructing Lists
Post by: CAB on June 09, 2005, 09:07:28 AM
You could create an intermediate routine to split like this
Code: [Select]
(defun split (fun arg)
  (if (= (length arg) 2)
    (fun (car arg) (cadr arg)) ; **  see note
    (prompt "\n**  Error in data - expected list of two items")
  )
)

**  This will not work but I forgot how to make a variable act as a function.
Use EVAL I think?? Never used it but I did see it somewhere.

The call would be
Code: [Select]
(split myroutine_name '((1 2 3)(4 5 6)) )

But how hard is it to use
Code: [Select]
(myroutine_name (car ((1 2 3)(4 5 6))) ((cadr ((1 2 3)(4 5 6)))) )
Title: Deconstructing Lists
Post by: SMadsen on June 09, 2005, 09:10:18 AM
APPLY with the original list?

(apply 'another_function theList)
Title: Deconstructing Lists
Post by: CAB on June 09, 2005, 09:57:55 AM
Thanks Stig, good to see ya.

I guess my idea would not work then as you must pass both items as two arguments to satisfy the subroutine "MyRoutine arg1 arg2".
Title: Deconstructing Lists
Post by: whdjr on June 09, 2005, 10:00:35 AM
I prompt the user for a

Source Directory --> d:\test    
   
and a

Target Directory  --> d:\test 2

Then it returns all the folders in those two paths which I then remove the source and target folders from the paths:

Code: [Select]
(("\\test a" "\\test a\\test a1" "\\test a\\test a2" "\\test a\\test a3" "\\test c" "\\test c\\test c1")
 ("\\test a" "\\test a\\test a1" "\\test a\\test a2" "\\test b" "\\test b\\test b1" "\\test b\\test b2")
)

Then I will pass this list to another function to return only similar paths:
Code: [Select]
(defun Return_Same_Items (lst1 lst2)
  (vl-remove-if-not '(lambda (x) (member x lst2)) lst1)
)

This function however accepts two lists instead of one list.  I would rather not change this function because I use in other programs.

My delimma is how to get this
Code: [Select]
(("\\test a" "\\test a\\test a1" "\\test a\\test a2" "\\test a\\test a3" "\\test c" "\\test c\\test c1")
 ("\\test a" "\\test a\\test a1" "\\test a\\test a2" "\\test b" "\\test b\\test b1" "\\test b\\test b2")
)
into this
Code: [Select]
("\\test a" "\\test a\\test a1" "\\test a\\test a2" "\\test a\\test a3" "\\test c" "\\test c\\test c1")
("\\test a" "\\test a\\test a1" "\\test a\\test a2" "\\test b" "\\test b\\test b1" "\\test b\\test b2")
Comprende? :)
Title: Deconstructing Lists
Post by: whdjr on June 09, 2005, 10:03:45 AM
I wish I would have tried your post before I wrote my post Stig.  Thanks man that was the ticket. :lol:

Stig  --> 8)  and :loneranger:
Title: Deconstructing Lists
Post by: MP on June 09, 2005, 10:06:36 AM
I must be missing something because it seems no more ambitious than using car and cadr, ie --

(Return_Same_Items (car lst) (cadr lst))

Where lst equals --

Code: [Select]
(  ("\\test a" "\\test a\\test a1" "\\test a\\test a2" "\\test a\\test a3" "\\test c" "\\test c\\test c1")
   ("\\test a" "\\test a\\test a1" "\\test a\\test a2" "\\test b" "\\test b\\test b1" "\\test b\\test b2")
)

?
Title: Deconstructing Lists
Post by: whdjr on June 09, 2005, 10:13:02 AM
That does work MP, its just that I use that routine for other progs as well and I didn't want to change for just this one.  That was all.
Title: Deconstructing Lists
Post by: MP on June 09, 2005, 10:18:33 AM
Sorry, I'm pretty thick this morning, I still don't get it. What is it your changing or not changing? :oops:
Title: Deconstructing Lists
Post by: MP on June 09, 2005, 10:26:11 AM
Ok, I get it, you want to go this route --

(apply 'Return_Same_Items lst)

Rather than

(Return_Same_Items (car lst) (cadr lst))

Okidoki.
Title: Deconstructing Lists
Post by: whdjr on June 09, 2005, 10:29:54 AM
:D
Title: Deconstructing Lists
Post by: CAB on June 09, 2005, 10:37:37 AM
Hold on.
That doesn't wash with me.
Confused as usual.

Using apply like this
(apply 'Return_Same_Items lst)
is equivalent to
(Return_Same_Items (car lst))
(Return_Same_Items (cadr lst))

Or I am I totally nuts. :)

I thought your subroutine expected two arguments?
Title: Deconstructing Lists
Post by: MP on June 09, 2005, 10:40:24 AM
Close.

(apply 'Return_Same_Items lst)

Is equivalent to --

(Return_Same_Items (car lst) (cadr lst))

Let's now consider which is better --

(/ number 2.0) versus (* number 0.5)

Discuss.
Title: Deconstructing Lists
Post by: whdjr on June 09, 2005, 11:10:41 AM
Isn't that kinda like 'a glass half full' and 'a glass half empty'.

 :lol:
Title: Deconstructing Lists
Post by: MP on June 09, 2005, 11:12:15 AM
Ding ding ding!

(Though it's bereft any optimism context).
Title: Deconstructing Lists
Post by: CAB on June 09, 2005, 11:43:20 AM
OK so I'm crazy, but i did figure it out with your help. :)
Just wanted to see if i could do it with an indirect function call.

Code: [Select]
(DEFUN XYZ (A1 A2)
  (+ a1 a2)
)

(defun split (fun arg)
  (if (= (length arg) 2)
    (apply fun arg)
    (prompt "\n**  Error in data - expected list of two items")
  )
)

(defun c:test ()
  (split 'XYZ (list 3 5))
)