Author Topic: My head hurts.. problem with a list  (Read 7071 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

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: My head hurts.. problem with a list
« Reply #15 on: March 01, 2011, 02:16:47 PM »
Another:

Code: [Select]
(defun _sum ( l )
  (if (cadr l)
    (cons (car l) (_sum (cons (+ (car l) (cadr l)) (cddr l))))
    (list (car l))
  )
)

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: My head hurts.. problem with a list
« Reply #16 on: March 01, 2011, 05:08:16 PM »
Another, using gc:scan as defined here.

EDIT: rewrited gc:scan function so that the first argument is a 'quoted' function (as others 'high order' AutoLISP functions)
Code: [Select]
(defun gc:scan (fun acc lst)
  (cons acc (mapcar (function (lambda (x) (setq acc ((eval fun) acc x)))) lst))
)

(gc:scan '+ (car lst) (cdr lst))

F#
Code: [Select]
List.scan (+) lst.Head lst.Tail
« Last Edit: March 01, 2011, 05:45:41 PM by gile »
Speaking English as a French Frog

Brick_top

  • Guest
Re: My head hurts.. problem with a list
« Reply #17 on: March 02, 2011, 04:28:18 AM »
Thanks a lot for all the options!

I have a lot to study

Brick_top

  • Guest
Re: My head hurts.. problem with a list
« Reply #18 on: March 02, 2011, 07:04:30 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)

Hi, I just want to thank you for that last part "lst (cdr lst)" didn't know I could do that and it is helping me a lot in other problems I was having. I'm trying to understand what you can do with lambda and mapcar


ronjonp

  • Needs a day job
  • Posts: 7529
Re: My head hurts.. problem with a list
« Reply #19 on: March 02, 2011, 10:11:26 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)

Hi, I just want to thank you for that last part "lst (cdr lst)" didn't know I could do that and it is helping me a lot in other problems I was having. I'm trying to understand what you can do with lambda and mapcar


:-) Good deal ... if you have any questions don't hesitate to ask.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: My head hurts.. problem with a list
« Reply #20 on: March 02, 2011, 12:09:05 PM »
a shortened Ron's
Code: [Select]
(mapcar '+ (cons 0 lst) lst)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: My head hurts.. problem with a list
« Reply #21 on: March 02, 2011, 12:10:52 PM »


EDIT:  Wait a minute... I thought the result was meant to be as in post #5?
« Last Edit: March 02, 2011, 12:26:24 PM by Lee Mac »

ronjonp

  • Needs a day job
  • Posts: 7529
Re: My head hurts.. problem with a list
« Reply #22 on: March 02, 2011, 12:29:09 PM »
a shortened Ron's
Code: [Select]
(mapcar '+ (cons 0 lst) lst)

Knew you couldn't resist  :-P   .very nice BTW  8-)
« Last Edit: March 02, 2011, 12:37:07 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Brick_top

  • Guest
Re: My head hurts.. problem with a list
« Reply #23 on: March 02, 2011, 01:48:17 PM »
a shortened Ron's
Code: [Select]
(mapcar '+ (cons 0 lst) lst)

Is this for real?  :-o   :-o

Don't have autocad right now

Brick_top

  • Guest
Re: My head hurts.. problem with a list
« Reply #24 on: March 02, 2011, 01:55:08 PM »


EDIT:  Wait a minute... I thought the result was meant to be as in post #5?

yeah, it is as in post #5

btw, thanks for your solution but it is way over my head as of now as is gile's
« Last Edit: March 02, 2011, 02:04:39 PM by Brick_top »

ronjonp

  • Needs a day job
  • Posts: 7529
Re: My head hurts.. problem with a list
« Reply #25 on: March 02, 2011, 02:20:03 PM »
a shortened Ron's
Code: [Select]
(mapcar '+ (cons 0 lst) lst)

Is this for real?  :-o   :-o

Don't have autocad right now

That gives the same result as my "wrong" answer ... but nonetheless cool.  :-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: My head hurts.. problem with a list
« Reply #26 on: March 02, 2011, 02:23:25 PM »
EDIT:  Wait a minute... I thought the result was meant to be as in post #5?

yeah, it is as in post #5

Nope...

Code: [Select]
_$ (mapcar '+ (cons 0 '(10 20 30 40 50)) '(10 20 30 40 50))
(10 30 50 70 90)

From your description in post#5, correct result should be:

Code: [Select]
(10 30 60 100 150)

Guitar_Jones

  • Guest
Re: My head hurts.. problem with a list
« Reply #27 on: March 02, 2011, 02:55:49 PM »
Another
Code: [Select]
(defun _sum (l)(mapcar '(lambda (x) (apply '+ (member x (reverse l)))) l))

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: My head hurts.. problem with a list
« Reply #28 on: March 02, 2011, 03:12:23 PM »
Another
Code: [Select]
(defun _sum (l)(mapcar '(lambda (x) (apply '+ (member x (reverse l)))) l))

Nice idea, but be careful for duplicate elements!  :lol:

Guitar_Jones

  • Guest
Re: My head hurts.. problem with a list
« Reply #29 on: March 02, 2011, 03:16:44 PM »
Thanks Lee. :ugly:

Brick_top

  • Guest
Re: My head hurts.. problem with a list
« Reply #30 on: March 02, 2011, 03:46:42 PM »
EDIT:  Wait a minute... I thought the result was meant to be as in post #5?

yeah, it is as in post #5

Nope...

Code: [Select]
_$ (mapcar '+ (cons 0 '(10 20 30 40 50)) '(10 20 30 40 50))
(10 30 50 70 90)

From your description in post#5, correct result should be:

Code: [Select]
(10 30 60 100 150)

Sorry my english tricked me I meant to say "yeah it is meant to be as in post #5"

Brick_top

  • Guest
Re: My head hurts.. problem with a list
« Reply #31 on: March 02, 2011, 03:48:56 PM »
a shortened Ron's
Code: [Select]
(mapcar '+ (cons 0 lst) lst)

Is this for real?  :-o   :-o

Don't have autocad right now

That gives the same result as my "wrong" answer ... but nonetheless cool.  :-)

Impressive anyway.. for me