TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started 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
-
Try this
(defun newlist (l / sum)
(setq sum 0)
(mapcar (function (lambda (e) (setq sum (+ e sum)))) l)
)
-
Oh... it was simpler than I though
I figured out a way to do it
(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!
-
Try this
(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?
-
Here's one way to do it:
(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)
-
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:
(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)
-
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:
(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-)
-
At least its good to know that my english is not that bad after all :laugh:
-
phanaem answered your question in post 2. Just pass you list to the ' NewList ' function.
Command: (NEWLIST '(10 20 30 40 50))
(10 30 60 100 150)
-
phanaem answered your question in post 2. Just pass you list to the ' NewList ' function.
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
-
Not pretty but ... Here's my other post modified to do what you want ...
A While version:
(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:
-
and Vovka is still looking.... :)
-
Here's my other post modified to do what you want ...
(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
-
Here's my other post modified to do what you want ...
(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 :-)
-
and Vovka is still looking.... :)
:-D
-
Another:
(defun _sum ( l )
(if (cadr l)
(cons (car l) (_sum (cons (+ (car l) (cadr l)) (cddr l))))
(list (car l))
)
)
-
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)
(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#
List.scan (+) lst.Head lst.Tail
-
Thanks a lot for all the options!
I have a lot to study
-
Here's one way to do it:
(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
-
Here's one way to do it:
(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.
-
a shortened Ron's
(mapcar '+ (cons 0 lst) lst)
-
(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)?
-
a shortened Ron's
(mapcar '+ (cons 0 lst) lst)
Knew you couldn't resist :-P .very nice BTW 8-)
-
a shortened Ron's
(mapcar '+ (cons 0 lst) lst)
Is this for real? :-o :-o
Don't have autocad right now
-
(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
-
a shortened Ron's
(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. :-)
-
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...
_$ (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:
(10 30 60 100 150)
-
Another
(defun _sum (l)(mapcar '(lambda (x) (apply '+ (member x (reverse l)))) l))
-
Another
(defun _sum (l)(mapcar '(lambda (x) (apply '+ (member x (reverse l)))) l))
Nice idea, but be careful for duplicate elements! :lol:
-
Thanks Lee. :ugly:
-
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...
_$ (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:
(10 30 60 100 150)
Sorry my english tricked me I meant to say "yeah it is meant to be as in post #5"
-
a shortened Ron's
(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