Author Topic: My head hurts.. problem with a list  (Read 7073 times)

0 Members and 1 Guest are viewing this topic.

Brick_top

  • Guest
My head hurts.. problem with a list
« on: March 01, 2011, 09:04:52 AM »
Hi, I have a list of several values for example

(10.2757 12.1581 33.2103)

But this list can have a different number of items.

I want to create a new list which would be:

the first element of the original list , and then every next element should be the sum of itself plus the sum of every other item prior to it.

Thanks
« Last Edit: March 01, 2011, 12:03:38 PM by Brick_top »

Stefan

  • Bull Frog
  • Posts: 319
  • The most I miss IRL is the Undo button
Re: My head hurts.. problem with a list
« Reply #1 on: March 01, 2011, 09:19:40 AM »
Try this

Code: [Select]
(defun newlist (l / sum)
  (setq sum 0)
  (mapcar (function (lambda (e) (setq sum (+ e sum)))) l)
  )

Brick_top

  • Guest
Re: My head hurts.. problem with a list
« Reply #2 on: March 01, 2011, 09:27:49 AM »
Oh... it was simpler than I though

I figured out a way to do it

Code: [Select]
(repeat (- (length dl2) 1)
    (setq dl3 (apply '+ dl2))
    (setq dl4 (append (list dl3) dl4))
    (setq dl2 (cdr dl2))
 )

where dl2 is the list, which I have to reverse first

I just have to add the first element when the loop is ended

thanks a lot for your solution aswell!

« Last Edit: March 01, 2011, 12:04:18 PM by Brick_top »

Brick_top

  • Guest
Re: My head hurts.. problem with a list
« Reply #3 on: March 01, 2011, 09:55:47 AM »
Try this

Code: [Select]
(defun newlist (l / sum)
  (setq sum 0)
  (mapcar (function (lambda (e) (setq sum (+ e sum)))) l)
  )

am I doing something wrong or is your function making a sum of all the elements in the list?

ronjonp

  • Needs a day job
  • Posts: 7529
Re: My head hurts.. problem with a list
« Reply #4 on: March 01, 2011, 11:39:05 AM »
Here's one way to do it:

Code: [Select]
(setq lst '(10.2757 12.1581 33.2103 45.11 1.1 2.2 3.3))
(cons (car lst) (mapcar (function (lambda (l1 l2) (+ l1 l2))) lst (cdr lst)))

;;returns
;;(10.2757 22.4338 45.3684 78.3203 46.21 3.3 5.5)
« Last Edit: March 01, 2011, 11:44:24 AM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Brick_top

  • Guest
Re: My head hurts.. problem with a list
« Reply #5 on: March 01, 2011, 11:59:29 AM »
Having watched your expertize in other threads I guess I must have not explained myself very well, I'll try again.

example of original list (10 20 30 40 50)

the list I wanted was:
element nº 1 = 10
element nº 2 = (20 + 10)
element nº 3 = (30 + 20 + 10)
element nº 4 = (40 + 30 + 20 + 10)
element nº 5 = (50 + 40 + 30 + 20 + 10)

But anyway my way of doing it is working as I want, thanks a lot for chiming in!

Here's one way to do it:

Code: [Select]
(setq lst '(10.2757 12.1581 33.2103 45.11 1.1 2.2 3.3))
(cons (car lst) (mapcar (function (lambda (l1 l2) (+ l1 l2))) lst (cdr lst)))

;;returns
;;(10.2757 22.4338 45.3684 78.3203 46.21 3.3 5.5)

ronjonp

  • Needs a day job
  • Posts: 7529
Re: My head hurts.. problem with a list
« Reply #6 on: March 01, 2011, 12:04:02 PM »
Having watched your expertize in other threads I guess I must have not explained myself very well, I'll try again.

example of original list (10 20 30 40 50)

the list I wanted was:
element nš 1 = 10
element nš 2 = (20 + 10)
element nš 3 = (30 + 20 + 10)
element nš 4 = (40 + 30 + 20 + 10)
element nš 5 = (50 + 40 + 30 + 20 + 10)

But anyway my way of doing it is working as I want, thanks a lot for chiming in!

Here's one way to do it:

Code: [Select]
(setq lst '(10.2757 12.1581 33.2103 45.11 1.1 2.2 3.3))
(cons (car lst) (mapcar (function (lambda (l1 l2) (+ l1 l2))) lst (cdr lst)))

;;returns
;;(10.2757 22.4338 45.3684 78.3203 46.21 3.3 5.5)

After reading closer "sum of every other item prior to it" I was way off  :-D Sorry ... looks like Vovka is looking at thread ... I'm sure he'll have a one liner for you  8-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Brick_top

  • Guest
Re: My head hurts.. problem with a list
« Reply #7 on: March 01, 2011, 12:07:11 PM »
At least its good to know that my english is not that bad after all  :laugh:

T.Willey

  • Needs a day job
  • Posts: 5251
Re: My head hurts.. problem with a list
« Reply #8 on: March 01, 2011, 12:08:22 PM »
phanaem answered your question in post 2.  Just pass you list to the ' NewList ' function.

Quote
Command: (NEWLIST '(10 20 30 40 50))
(10 30 60 100 150)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Brick_top

  • Guest
Re: My head hurts.. problem with a list
« Reply #9 on: March 01, 2011, 12:09:25 PM »
phanaem answered your question in post 2.  Just pass you list to the ' NewList ' function.

Quote
Command: (NEWLIST '(10 20 30 40 50))
(10 30 60 100 150)

Must have done something wrong while trying the function then

Thanks a lot

ronjonp

  • Needs a day job
  • Posts: 7529
Re: My head hurts.. problem with a list
« Reply #10 on: March 01, 2011, 12:09:43 PM »
Not pretty but ... Here's my other post modified to do what you want ...

A While version:

Code: [Select]
(defun whee (lst / n out x)
  (setq n 0.)
  (while (setq x (car lst))
    (setq out (cons (setq n (+ x n)) out)
 lst (cdr lst)
    )
  )
  (reverse out)
)
(whee '(10.2757 12.1581 33.2103 45.11 1.1 2.2 3.3))

*Edit .. had CDR in wrong place  .. man I need more coffee  :oops:
« Last Edit: March 01, 2011, 12:28:37 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: My head hurts.. problem with a list
« Reply #11 on: March 01, 2011, 12:13:24 PM »
and Vovka is still looking.... :)

Brick_top

  • Guest
Re: My head hurts.. problem with a list
« Reply #12 on: March 01, 2011, 12:13:45 PM »
Here's my other post modified to do what you want ...


Code: [Select]
(setq tmp 0.
      lst '(10.2757 12.1581 33.2103 45.11 1.1 2.2 3.3)
)
(cons (car lst) (mapcar (function (lambda (l1 l2) (+ (setq tmp (+ l1 tmp)) l2))) lst (cdr lst)))

;;returns
;;(10.2757 22.4338 55.6441 100.754 101.854 104.054 107.354)

thanks a lot, its good to learn how others think.

Unfortunately my solution is not that elegant

ronjonp

  • Needs a day job
  • Posts: 7529
Re: My head hurts.. problem with a list
« Reply #13 on: March 01, 2011, 12:25:39 PM »
Here's my other post modified to do what you want ...


Code: [Select]
(setq tmp 0.
      lst '(10.2757 12.1581 33.2103 45.11 1.1 2.2 3.3)
)
(cons (car lst) (mapcar (function (lambda (l1 l2) (+ (setq tmp (+ l1 tmp)) l2))) lst (cdr lst)))

;;returns
;;(10.2757 22.4338 55.6441 100.754 101.854 104.054 107.354)

thanks a lot, its good to learn how others think.

Unfortunately my solution is not that elegant

I modified the code above ... should be a bit faster  :-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ronjonp

  • Needs a day job
  • Posts: 7529
Re: My head hurts.. problem with a list
« Reply #14 on: March 01, 2011, 12:25:58 PM »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC