Jump to content

Recommended Posts

Posted

Just another thought building on what I added earlier. If you still wanted to keep the last element in the first list, you could add a 0 to the end of the second list to be added to the first list. =>

(setq lst (list 1 2 3 4 5 6 7 8 9))
;returns (1 2 3 4 5 6 7 8 9)
(mapcar '+ lst (reverse (cons 0 (reverse (cdr lst)))))
;returns (3 5 7 9 11 13 15 17 9)

you could even loop around to the beginning of the list and add the first element to the last =>

(mapcar '+ lst (reverse (cons (car lst) (reverse (cdr lst)))))
;returns (3 5 7 9 11 13 15 17 10)

  • Replies 22
  • Created
  • Last Reply

Top Posters In This Topic

  • pBe

    5

  • MSasu

    3

  • Tharwat

    3

  • LibertyOne

    3

Top Posters In This Topic

Posted

Apologies but this task appears to be made overcomplicated - refer to my post #17.

Posted
Apologies but this task appears to be made overcomplicated - refer to my post #17.

 

true, but like you also said is that the result wasn't really clearly defined in the beginning of the thread. I was solving more on the lines of list manipulation by returning a modified list and not just that of an equation by adding up a list of numbers. What it all boils down to is what you want as a result and how generic can you build a function to suit different needs of many different programs.

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