Jump to content

Recommended Posts

Posted

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.

  • Replies 22
  • Created
  • Last Reply

Top Posters In This Topic

  • pBe

    5

  • MSasu

    3

  • Tharwat

    3

  • LibertyOne

    3

Top Posters In This Topic

Posted (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 by MSasu
Posted

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

Posted

(mapcar '(lambda (x) (+ x (1+ x))) '(1 2 3 4 5 6 7 8 9))

Posted

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))

Posted

I wonder !!!!!

 

Does the OP want to sum the first umber with the second one ?

 

Or dividing the list into separated lists ?

 

Thanks .

Posted

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) (8 + 9))
  • pBe: (3 5 7 9 11 13 15 17 19)

Regards,

Mircea

Posted

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 :lol:

Posted

@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,

Posted
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 :lol:

 

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.

Posted

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.

Posted
@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

Posted
(setq l '(1 2 3 4 5 6 7 8 9))

(* 2 (apply '+ l))

 

sweet!

 

8)

Posted

I insist on a list. :D

 

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)

Posted
(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

Posted
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.

Posted
(defun foo (lst) (apply '+ (mapcar '+ lst (cdr lst))))

:unsure:

Posted

@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

Posted

Kumar,

where are you?

All these activities are for your question!

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...