Author Topic: List Manipulation Help  (Read 939 times)

0 Members and 1 Guest are viewing this topic.

abe123456789

  • Newt
  • Posts: 24
List Manipulation Help
« on: April 06, 2023, 09:17:18 PM »
Hello,

How can I achieve the desired list using lisp?

starting list
lst = 1 2 3 4 5 6 7

Desired list
lst = (1 2) (2 3) (3 4) (4 5) (5 6) (6 7)

Thank you,


« Last Edit: April 06, 2023, 09:24:23 PM by abe123456789 »

BIGAL

  • Swamp Rat
  • Posts: 1429
  • 40 + years of using Autocad
Re: List Manipulation Help
« Reply #1 on: April 06, 2023, 11:03:18 PM »
Something like this I will let you have a go.

x 0
repeat (- (length lst) 1)
(cons (nth x lst)(nth (+ x 1) lst))
(1+ x)
A man who never made a mistake never made anything

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: List Manipulation Help
« Reply #2 on: April 07, 2023, 04:43:14 AM »
Code - Auto/Visual Lisp: [Select]
  1. (setq lst '(1 2 3 4 5 6 7))
  2.  
  3. (and (< 1 (length lst))
  4.      (setq 1st (car lst))
  5.      (while (setq lst (cdr lst))
  6.        (setq rtn (cons (list 1st (setq 1st (car lst))) rtn))
  7.        )
  8.      )
  9. (reverse rtn)
  10.  

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: List Manipulation Help
« Reply #3 on: April 07, 2023, 04:48:45 AM »
Code - Auto/Visual Lisp: [Select]
  1. (setq lst '(1 2 3 4 5 6 7))
  2. (mapcar 'list lst (cdr lst)))
Speaking English as a French Frog

abe123456789

  • Newt
  • Posts: 24
Re: List Manipulation Help
« Reply #4 on: April 07, 2023, 09:37:18 AM »
Thanks for the help guys. :-)

Jim2018

  • Mosquito
  • Posts: 16
Re: List Manipulation Help
« Reply #5 on: April 09, 2023, 05:12:58 AM »
Sorry gile
but I don't understand the function of the command (mapcar 'list lst (cdr lst))).
Normally the result should be different as follows ((1 2 3 4 5 6 7) (2 3 4 5 6 7) (3 4 5 6 7) (4 5 6 7) (5 6 7) (6 7))
and yet it is not!
Why when we put (cdr lst) does not result in all the elements except the first one?
Also why (marcar 'list (cdr lst)) has as a result ((2) (3) (4) (5) (6) (7)) and not  ((2 3 4 5 6 7) (3 4 5 6 7) (4 5 6 7) (5 6 7)(6 7))?

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: List Manipulation Help
« Reply #6 on: April 09, 2023, 05:55:48 AM »
Hi,

From the docs: the mapcar function "Returns a list that is the result of executing a function with a list (or lists) supplied as arguments to the function"
So, assuming lst is equal to '(1 2 3 4 5 6 7), (mapcar 'list lst)  returns: ((1) (2) (3) (4) (5) (6) (7)) because it returns a list that is the result of executing the list function to all items in lst.
Which is equivalent to: (list (list 1) (list 2) (list 3) (list 4) (list 5) (list 6) (list 7))

Same thing for: (mapcar 'list (cdr lst)) which is equivalent to (list (list 2) (list 3) (list 4) (list 5) (list 6) (list 7)) and return ((2) (3) (4) (5) (6) (7))

When mapcar is called with more than one list, the supplied function (list function here) is applied to all corresponding items of the lists while none of the list is empty.
(mapcar 'list lst (cdr lst)) is equivalent to: (list (list 1 2) (list 2 3) (list 3 4) (list 4 5) (list 5 6) (list 6 7)) which returns ((1 2) (2 3) (3 4) (4 5) (5 6) (6 7)).
Speaking English as a French Frog

Jim2018

  • Mosquito
  • Posts: 16
Re: List Manipulation Help
« Reply #7 on: April 09, 2023, 09:39:06 AM »
Hi gile
I know the way the mapcar command works
I don't understand why (cdr lst) would have this effect.
How does it happen when the mapcar calculates for the first element (cdr lst) returns the value of the second element and so on. I can't understand that.
The cdr command when applied to a list generally returns the list minus the first element.

Thank you very much for your time

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: List Manipulation Help
« Reply #8 on: April 09, 2023, 10:14:00 AM »
(mapcar 'list lst (cdr lst))
stands for:
(mapcar 'list '(1 2 3 4 5 6 7) '(2 3 4 5 6 7))
which is similar to:
(list (list 1 2) (list 2 3) (list 3 4) (list 4 5) (list 5 6) (list 6 7))
and returns:
((1 2) (2 3) (3 4) (4 5) (5 6) (6 7))
Speaking English as a French Frog

Jim2018

  • Mosquito
  • Posts: 16
Re: List Manipulation Help
« Reply #9 on: April 09, 2023, 10:55:46 AM »
Thank you gile
I realized my mistake. I calculated the (cdr lst) over and over for each element of the list.
The mapcar iterates through the lst and its (cdr lst), so simple!