Author Topic: Deconstructing Lists  (Read 2545 times)

0 Members and 1 Guest are viewing this topic.

whdjr

  • Guest
Deconstructing Lists
« 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?

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Deconstructing Lists
« Reply #1 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 .
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

whdjr

  • Guest
Deconstructing Lists
« Reply #2 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.

MP

  • Seagull
  • Posts: 17529
Deconstructing Lists
« Reply #3 on: June 08, 2005, 06:03:27 PM »
Sample data and desired output please.

:)
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9300
Deconstructing Lists
« Reply #4 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.
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

CAB

  • Global Moderator
  • Seagull
  • Posts: 10376
Deconstructing Lists
« Reply #5 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)))) )
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

SMadsen

  • Guest
Deconstructing Lists
« Reply #6 on: June 09, 2005, 09:10:18 AM »
APPLY with the original list?

(apply 'another_function theList)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10376
Deconstructing Lists
« Reply #7 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".
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

whdjr

  • Guest
Deconstructing Lists
« Reply #8 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? :)

whdjr

  • Guest
Deconstructing Lists
« Reply #9 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:

MP

  • Seagull
  • Posts: 17529
Deconstructing Lists
« Reply #10 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")
)

?
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

whdjr

  • Guest
Deconstructing Lists
« Reply #11 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.

MP

  • Seagull
  • Posts: 17529
Deconstructing Lists
« Reply #12 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:
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

MP

  • Seagull
  • Posts: 17529
Deconstructing Lists
« Reply #13 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.
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

whdjr

  • Guest
Deconstructing Lists
« Reply #14 on: June 09, 2005, 10:29:54 AM »
:D

CAB

  • Global Moderator
  • Seagull
  • Posts: 10376
Deconstructing Lists
« Reply #15 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?
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17529
Deconstructing Lists
« Reply #16 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.
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

whdjr

  • Guest
Deconstructing Lists
« Reply #17 on: June 09, 2005, 11:10:41 AM »
Isn't that kinda like 'a glass half full' and 'a glass half empty'.

 :lol:

MP

  • Seagull
  • Posts: 17529
Deconstructing Lists
« Reply #18 on: June 09, 2005, 11:12:15 AM »
Ding ding ding!

(Though it's bereft any optimism context).
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

CAB

  • Global Moderator
  • Seagull
  • Posts: 10376
Deconstructing Lists
« Reply #19 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))
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.