giskumar Posted April 19, 2011 Posted April 19, 2011 Hi all, I have a list that contains numerical values like '(1 2 3 4 5 6 7 8 9) I want to add each value with next value like (1+2)+(2+3)+(3+4)+(4+5)+....... Is there a way to this with lambda function? Thanks, Kumar. Quote
MSasu Posted April 19, 2011 Posted April 19, 2011 (edited) How about this way (taking care that each list entry, except extremities, is summed twice)? (setq MyList '(1 2 3 4 5 6 7 8 9)) (setq theSum (+ (car MyList) (last MyList))) ;sumate extremities (foreach Item (cdr (reverse (cdr MyList))) ;parse list without extremities (setq theSum (+ theSum (* 2 Item))) ;sumate twice the intermediate items ) Regards, Mircea Edited April 19, 2011 by MSasu Quote
Tharwat Posted April 19, 2011 Posted April 19, 2011 Another way and do not except singular number of list to avoid cons number with nil . (setq lst '(1 2 3 4 5 6 7 8 9 )) (setq i 0 j 1 ) (if (not (eq (rem (/ (vl-list-length lst) 2) 2) 2)) (setq l (- (vl-list-length lst) 1)) (setq l (vl-list-length lst)) ) (repeat l (setq one (cons (list (nth i lst) '+ (nth j lst)) one) ) (setq i (1+ i) j (1+ j) ) (reverse one) ) TharwaT Quote
pBe Posted April 19, 2011 Posted April 19, 2011 (mapcar '(lambda (x) (+ x (1+ x))) '(1 2 3 4 5 6 7 8 9)) Quote
Ahankhah Posted April 19, 2011 Posted April 19, 2011 Kumar, here is another short code: (setq MyList '(1 2 3 4 5 6 7 8 9)) (setq MyList (append (cdr (reverse (cdr MyList))) MyList) ) (setq TheSum (apply '+ MyList)) Quote
Tharwat Posted April 19, 2011 Posted April 19, 2011 I wonder !!!!! Does the OP want to sum the first umber with the second one ? Or dividing the list into separated lists ? Thanks . Quote
MSasu Posted April 19, 2011 Posted April 19, 2011 Will require some extra clarification from OP since seems that each of us understood differently his request: msasu: 80 Tharwat: ((1 + 2) (2 + 3) (3 + 4) (4 + 5) (5 + 6) (6 + 7) (7 + (8 + 9)) pBe: (3 5 7 9 11 13 15 17 19) Regards, Mircea Quote
pBe Posted April 19, 2011 Posted April 19, 2011 I guess you're right well. (apply '+ (mapcar '(lambda (x) (+ x (1+ x))) '(1 2 3 4 5 6 7 8 9))) Now i'm confuse Quote
MSasu Posted April 19, 2011 Posted April 19, 2011 @pBe: I think by using your solution the list need to be parsed without last item: (cdr (reverse (cdr '(1 2 3 4 5 6 7 8 9)))) Regards, Quote
Ahankhah Posted April 19, 2011 Posted April 19, 2011 I guess you're right well. (apply '+ (mapcar '(lambda (x) (+ x (1+ x))) '(1 2 3 4 5 6 7 8 9))) Now i'm confuse I think the list is an example so sequential numbers must not be considered for writing the code. By the way, the result is 99 and not 80. Quote
pBe Posted April 19, 2011 Posted April 19, 2011 ok then The original question is how to code it using lambda, hence this (apply '+ (cdr (reverse (mapcar '(lambda (x) (+ x (1+ x))) '(1 2 3 4 5 6 7 8 9))))) 80 it is. Quote
pBe Posted April 19, 2011 Posted April 19, 2011 @pBe: I think by using your solution the list need to be parsed without last item: (cdr (reverse (cdr '(1 2 3 4 5 6 7 8 9)))) Regards, Correct. Done (see previous post) no need for double cdr Thanks msasu Quote
Lee Mac Posted April 19, 2011 Posted April 19, 2011 (setq l '(1 2 3 4 5 6 7 8 9)) (* 2 (apply '+ l)) Quote
pBe Posted April 19, 2011 Posted April 19, 2011 (setq l '(1 2 3 4 5 6 7 8 9)) (* 2 (apply '+ l)) sweet! Quote
Tharwat Posted April 19, 2011 Posted April 19, 2011 I insist on a list. Here it is with lambda , But the last list would be built with nil . ((lambda (lst l i j / one) (repeat l (setq one (cons (list (nth (setq i (1+ i)) lst) '+ (nth (setq j (1+ j)) lst) ) one ) ) (reverse one) ) ) (setq lst '(1 2 3 4 5 6 7 8 9)) (- (vl-list-length lst) 1) 0 1 ) (print one) Quote
David Bethel Posted April 19, 2011 Posted April 19, 2011 (setq l '(1 2 3 4 5 6 7 8 9)) (* 2 (apply '+ l)) Lee, I don't think this is correct [b][color=BLACK]([/color][/b]setq l '[b][color=FUCHSIA]([/color][/b]1 2 3 4 5 6 7 8 9[b][color=FUCHSIA])[/color][/b] s 0[b][color=BLACK])[/color][/b] [color=#8b4513];;;LEE MAC[/color] [b][color=BLACK]([/color][/b]setq lm [b][color=FUCHSIA]([/color][/b]* 2 [b][color=NAVY]([/color][/b]apply '+ l[b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b] [b][color=BLACK]([/color][/b]while [b][color=FUCHSIA]([/color][/b]> [b][color=NAVY]([/color][/b]length l[b][color=NAVY])[/color][/b] 1[b][color=FUCHSIA])[/color][/b] [b][color=FUCHSIA]([/color][/b]setq s [b][color=NAVY]([/color][/b]+ s [b][color=MAROON]([/color][/b]nth 0 l[b][color=MAROON])[/color][/b] [b][color=MAROON]([/color][/b]nth 1 l[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b] l [b][color=NAVY]([/color][/b]cdr l[b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b] [b][color=BLACK]([/color][/b]princ [b][color=FUCHSIA]([/color][/b]strcat [color=#2f4f4f]"\n"[/color] [b][color=NAVY]([/color][/b]rtos s 2 0[b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b] [b][color=BLACK]([/color][/b]princ [b][color=FUCHSIA]([/color][/b]strcat [color=#2f4f4f]"\n"[/color] [b][color=NAVY]([/color][/b]rtos lm 2 0[b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b] It will depend if the OP's correct answer would be 80 or 90. It didn't specify it the first and last were to combined as well. -David Quote
Lee Mac Posted April 19, 2011 Posted April 19, 2011 Lee, I don't think this is correct It will depend if the OP's correct answer would be 80 or 90. It didn't specify it the first and last were to combined as well. -David That was the ambiguity that I wasn't sure of, which is also raised by others earlier in the thread. The question is also obscured by the unnecessary use of parentheses since addition is associative. Otherwise, some variants: _$ (setq l '(1 2 3 4 5 6 7 8 9)) (1 2 3 4 5 6 7 8 9) _$ _$ (* 2 (apply '+ l)) 90 _$ (- (* 2 (apply '+ l)) (car l) (last l)) 80 _$ (apply '+ (mapcar '+ l (cdr l))) 80 Also, I believe the list provided should be viewed as arbitrary, hence code should not rely upon elements being consecutive integers. Quote
alanjt Posted April 19, 2011 Posted April 19, 2011 (defun foo (lst) (apply '+ (mapcar '+ lst (cdr lst)))) Quote
LibertyOne Posted April 19, 2011 Posted April 19, 2011 @alanjt I came up with (setq lst '(1 2 3 4 5 6 7 8 9)) ;returns (1 2 3 4 5 6 7 8 9) (mapcar '+ lst (cdr lst)) ;returns (3 5 7 9 11 13 15 17) just as well, but remember that by adding two integer lists together which have different lengths, the smaller of the two will be returned. The original list has 9 elements, the returned list only 8 Quote
Ahankhah Posted April 20, 2011 Posted April 20, 2011 Kumar, where are you? All these activities are for your question! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.