TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Brick_top on March 01, 2011, 09:04:52 AM

Title: My head hurts.. problem with a list
Post by: Brick_top 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
Title: Re: My head hurts.. problem with a list
Post by: Stefan 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)
  )
Title: Re: My head hurts.. problem with a list
Post by: Brick_top 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!

Title: Re: My head hurts.. problem with a list
Post by: Brick_top 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?
Title: Re: My head hurts.. problem with a list
Post by: ronjonp 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)
Title: Re: My head hurts.. problem with a list
Post by: Brick_top 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)
Title: Re: My head hurts.. problem with a list
Post by: ronjonp 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-)
Title: Re: My head hurts.. problem with a list
Post by: Brick_top on March 01, 2011, 12:07:11 PM
At least its good to know that my english is not that bad after all  :laugh:
Title: Re: My head hurts.. problem with a list
Post by: T.Willey 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)
Title: Re: My head hurts.. problem with a list
Post by: Brick_top 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
Title: Re: My head hurts.. problem with a list
Post by: ronjonp 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:
Title: Re: My head hurts.. problem with a list
Post by: VovKa on March 01, 2011, 12:13:24 PM
and Vovka is still looking.... :)
Title: Re: My head hurts.. problem with a list
Post by: Brick_top 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
Title: Re: My head hurts.. problem with a list
Post by: ronjonp 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  :-)
Title: Re: My head hurts.. problem with a list
Post by: ronjonp on March 01, 2011, 12:25:58 PM
and Vovka is still looking.... :)
:-D

Title: Re: My head hurts.. problem with a list
Post by: Lee Mac 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))
  )
)
Title: Re: My head hurts.. problem with a list
Post by: gile on March 01, 2011, 05:08:16 PM
Another, using gc:scan as defined here (http://www.theswamp.org/index.php?topic=36653.msg421132#msg421132).

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
Title: Re: My head hurts.. problem with a list
Post by: Brick_top on March 02, 2011, 04:28:18 AM
Thanks a lot for all the options!

I have a lot to study
Title: Re: My head hurts.. problem with a list
Post by: Brick_top 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

Title: Re: My head hurts.. problem with a list
Post by: ronjonp 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.
Title: Re: My head hurts.. problem with a list
Post by: VovKa on March 02, 2011, 12:09:05 PM
a shortened Ron's
Code: [Select]
(mapcar '+ (cons 0 lst) lst)
Title: Re: My head hurts.. problem with a list
Post by: Lee Mac on March 02, 2011, 12:10:52 PM
(http://www.theswamp.org/screens/leemac/Bowing.gif)

EDIT:  Wait a minute... I thought the result was meant to be as in post #5 (http://www.theswamp.org/index.php?topic=37285.msg422850#msg422850)?
Title: Re: My head hurts.. problem with a list
Post by: ronjonp 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-)
Title: Re: My head hurts.. problem with a list
Post by: Brick_top 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
Title: Re: My head hurts.. problem with a list
Post by: Brick_top on March 02, 2011, 01:55:08 PM
(http://www.theswamp.org/screens/leemac/Bowing.gif)

EDIT:  Wait a minute... I thought the result was meant to be as in post #5 (http://www.theswamp.org/index.php?topic=37285.msg422850#msg422850)?

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
Title: Re: My head hurts.. problem with a list
Post by: ronjonp 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.  :-)
Title: Re: My head hurts.. problem with a list
Post by: Lee Mac on March 02, 2011, 02:23:25 PM
EDIT:  Wait a minute... I thought the result was meant to be as in post #5 (http://www.theswamp.org/index.php?topic=37285.msg422850#msg422850)?

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)
Title: Re: My head hurts.. problem with a list
Post by: Guitar_Jones on March 02, 2011, 02:55:49 PM
Another
Code: [Select]
(defun _sum (l)(mapcar '(lambda (x) (apply '+ (member x (reverse l)))) l))
Title: Re: My head hurts.. problem with a list
Post by: Lee Mac 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:
Title: Re: My head hurts.. problem with a list
Post by: Guitar_Jones on March 02, 2011, 03:16:44 PM
Thanks Lee. :ugly:
Title: Re: My head hurts.. problem with a list
Post by: Brick_top on March 02, 2011, 03:46:42 PM
EDIT:  Wait a minute... I thought the result was meant to be as in post #5 (http://www.theswamp.org/index.php?topic=37285.msg422850#msg422850)?

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"
Title: Re: My head hurts.. problem with a list
Post by: Brick_top 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