How can I sum the middle elements of an expanding list in Haskell?












-2















So far I know how to expand a list from its ends, but they end up getting doubled because of the first condition, which is to double a singleton. Would it make sense for the code to be like this:



sumExpand :: [Integer] -> [Integer]

sumExpand l = expand l
where
expand a = a
expand (x:) a = x: expand (x:a)
expand (x:xs) a = expand (x:a) (expand xs a)


And for me to work on its output:



[1,1,2,2,3,3] from [1,2,3]
instead of [1,3,5,3]


The latter being my desire? Here's how I got to a temporary solution for a list of two elements:



expand (x:xs) a = x: tail (expand (map (x+) xs) (last xs:a))




Output:



*Main> sumExpand [1,2]
[1,3,2]
*Main> sumExpand [1,2,3]
[1,7,4,3]




EDIT: basically, I want the algorithm to work like this: [a, b, c] => [a, a+b, b+c, c]










share|improve this question




















  • 1





    What precisely is your algorithm supposed to do? What are you having trouble with?

    – jberryman
    Nov 20 '18 at 17:01











  • You want to insert the sum of each adjacent pair between the pair? [a, b, c] => [a, a+b, b, b + c, c]?

    – chepner
    Nov 20 '18 at 17:17











  • What I want to do is the following: [a, b, c] => [a, a+b, b+c, c]. It's basically the same as what @chepner suggested, but without b repeating itself.

    – Marco_O
    Nov 20 '18 at 17:30








  • 1





    How about more than 3 elements in list? say, [a, b, c, d] => [a, a+b, b+c, c+d, d]?

    – assembly.jc
    Nov 20 '18 at 17:44











  • I have the solution, but could you give me any pointers on how to improve my research or problem solving skills, for when stuff like this happens? I just got down voted and I want to improve. Thanks in advance.

    – Marco_O
    Nov 20 '18 at 18:07
















-2















So far I know how to expand a list from its ends, but they end up getting doubled because of the first condition, which is to double a singleton. Would it make sense for the code to be like this:



sumExpand :: [Integer] -> [Integer]

sumExpand l = expand l
where
expand a = a
expand (x:) a = x: expand (x:a)
expand (x:xs) a = expand (x:a) (expand xs a)


And for me to work on its output:



[1,1,2,2,3,3] from [1,2,3]
instead of [1,3,5,3]


The latter being my desire? Here's how I got to a temporary solution for a list of two elements:



expand (x:xs) a = x: tail (expand (map (x+) xs) (last xs:a))




Output:



*Main> sumExpand [1,2]
[1,3,2]
*Main> sumExpand [1,2,3]
[1,7,4,3]




EDIT: basically, I want the algorithm to work like this: [a, b, c] => [a, a+b, b+c, c]










share|improve this question




















  • 1





    What precisely is your algorithm supposed to do? What are you having trouble with?

    – jberryman
    Nov 20 '18 at 17:01











  • You want to insert the sum of each adjacent pair between the pair? [a, b, c] => [a, a+b, b, b + c, c]?

    – chepner
    Nov 20 '18 at 17:17











  • What I want to do is the following: [a, b, c] => [a, a+b, b+c, c]. It's basically the same as what @chepner suggested, but without b repeating itself.

    – Marco_O
    Nov 20 '18 at 17:30








  • 1





    How about more than 3 elements in list? say, [a, b, c, d] => [a, a+b, b+c, c+d, d]?

    – assembly.jc
    Nov 20 '18 at 17:44











  • I have the solution, but could you give me any pointers on how to improve my research or problem solving skills, for when stuff like this happens? I just got down voted and I want to improve. Thanks in advance.

    – Marco_O
    Nov 20 '18 at 18:07














-2












-2








-2








So far I know how to expand a list from its ends, but they end up getting doubled because of the first condition, which is to double a singleton. Would it make sense for the code to be like this:



sumExpand :: [Integer] -> [Integer]

sumExpand l = expand l
where
expand a = a
expand (x:) a = x: expand (x:a)
expand (x:xs) a = expand (x:a) (expand xs a)


And for me to work on its output:



[1,1,2,2,3,3] from [1,2,3]
instead of [1,3,5,3]


The latter being my desire? Here's how I got to a temporary solution for a list of two elements:



expand (x:xs) a = x: tail (expand (map (x+) xs) (last xs:a))




Output:



*Main> sumExpand [1,2]
[1,3,2]
*Main> sumExpand [1,2,3]
[1,7,4,3]




EDIT: basically, I want the algorithm to work like this: [a, b, c] => [a, a+b, b+c, c]










share|improve this question
















So far I know how to expand a list from its ends, but they end up getting doubled because of the first condition, which is to double a singleton. Would it make sense for the code to be like this:



sumExpand :: [Integer] -> [Integer]

sumExpand l = expand l
where
expand a = a
expand (x:) a = x: expand (x:a)
expand (x:xs) a = expand (x:a) (expand xs a)


And for me to work on its output:



[1,1,2,2,3,3] from [1,2,3]
instead of [1,3,5,3]


The latter being my desire? Here's how I got to a temporary solution for a list of two elements:



expand (x:xs) a = x: tail (expand (map (x+) xs) (last xs:a))




Output:



*Main> sumExpand [1,2]
[1,3,2]
*Main> sumExpand [1,2,3]
[1,7,4,3]




EDIT: basically, I want the algorithm to work like this: [a, b, c] => [a, a+b, b+c, c]







list haskell sum expand






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 17:34







Marco_O

















asked Nov 20 '18 at 16:52









Marco_OMarco_O

167




167








  • 1





    What precisely is your algorithm supposed to do? What are you having trouble with?

    – jberryman
    Nov 20 '18 at 17:01











  • You want to insert the sum of each adjacent pair between the pair? [a, b, c] => [a, a+b, b, b + c, c]?

    – chepner
    Nov 20 '18 at 17:17











  • What I want to do is the following: [a, b, c] => [a, a+b, b+c, c]. It's basically the same as what @chepner suggested, but without b repeating itself.

    – Marco_O
    Nov 20 '18 at 17:30








  • 1





    How about more than 3 elements in list? say, [a, b, c, d] => [a, a+b, b+c, c+d, d]?

    – assembly.jc
    Nov 20 '18 at 17:44











  • I have the solution, but could you give me any pointers on how to improve my research or problem solving skills, for when stuff like this happens? I just got down voted and I want to improve. Thanks in advance.

    – Marco_O
    Nov 20 '18 at 18:07














  • 1





    What precisely is your algorithm supposed to do? What are you having trouble with?

    – jberryman
    Nov 20 '18 at 17:01











  • You want to insert the sum of each adjacent pair between the pair? [a, b, c] => [a, a+b, b, b + c, c]?

    – chepner
    Nov 20 '18 at 17:17











  • What I want to do is the following: [a, b, c] => [a, a+b, b+c, c]. It's basically the same as what @chepner suggested, but without b repeating itself.

    – Marco_O
    Nov 20 '18 at 17:30








  • 1





    How about more than 3 elements in list? say, [a, b, c, d] => [a, a+b, b+c, c+d, d]?

    – assembly.jc
    Nov 20 '18 at 17:44











  • I have the solution, but could you give me any pointers on how to improve my research or problem solving skills, for when stuff like this happens? I just got down voted and I want to improve. Thanks in advance.

    – Marco_O
    Nov 20 '18 at 18:07








1




1





What precisely is your algorithm supposed to do? What are you having trouble with?

– jberryman
Nov 20 '18 at 17:01





What precisely is your algorithm supposed to do? What are you having trouble with?

– jberryman
Nov 20 '18 at 17:01













You want to insert the sum of each adjacent pair between the pair? [a, b, c] => [a, a+b, b, b + c, c]?

– chepner
Nov 20 '18 at 17:17





You want to insert the sum of each adjacent pair between the pair? [a, b, c] => [a, a+b, b, b + c, c]?

– chepner
Nov 20 '18 at 17:17













What I want to do is the following: [a, b, c] => [a, a+b, b+c, c]. It's basically the same as what @chepner suggested, but without b repeating itself.

– Marco_O
Nov 20 '18 at 17:30







What I want to do is the following: [a, b, c] => [a, a+b, b+c, c]. It's basically the same as what @chepner suggested, but without b repeating itself.

– Marco_O
Nov 20 '18 at 17:30






1




1





How about more than 3 elements in list? say, [a, b, c, d] => [a, a+b, b+c, c+d, d]?

– assembly.jc
Nov 20 '18 at 17:44





How about more than 3 elements in list? say, [a, b, c, d] => [a, a+b, b+c, c+d, d]?

– assembly.jc
Nov 20 '18 at 17:44













I have the solution, but could you give me any pointers on how to improve my research or problem solving skills, for when stuff like this happens? I just got down voted and I want to improve. Thanks in advance.

– Marco_O
Nov 20 '18 at 18:07





I have the solution, but could you give me any pointers on how to improve my research or problem solving skills, for when stuff like this happens? I just got down voted and I want to improve. Thanks in advance.

– Marco_O
Nov 20 '18 at 18:07












1 Answer
1






active

oldest

votes


















4














Basically, all you want to compute component-wise sums between your input list and a shifted version of it:



a   b   c   d   e
a b c d e
---------------------------
a a+b b+c c+d d+e e


Fill each empty slot with a 0 (0:x and x++[0]), and you just need zipWith



> x -> zipWith (+) (0:x) (x++[0]) $ [1,2,3]
[1,3,5,3]





share|improve this answer
























  • Thank you! That's very useful. Now I have something to study its meaning.

    – Marco_O
    Nov 20 '18 at 17:50











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397811%2fhow-can-i-sum-the-middle-elements-of-an-expanding-list-in-haskell%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














Basically, all you want to compute component-wise sums between your input list and a shifted version of it:



a   b   c   d   e
a b c d e
---------------------------
a a+b b+c c+d d+e e


Fill each empty slot with a 0 (0:x and x++[0]), and you just need zipWith



> x -> zipWith (+) (0:x) (x++[0]) $ [1,2,3]
[1,3,5,3]





share|improve this answer
























  • Thank you! That's very useful. Now I have something to study its meaning.

    – Marco_O
    Nov 20 '18 at 17:50
















4














Basically, all you want to compute component-wise sums between your input list and a shifted version of it:



a   b   c   d   e
a b c d e
---------------------------
a a+b b+c c+d d+e e


Fill each empty slot with a 0 (0:x and x++[0]), and you just need zipWith



> x -> zipWith (+) (0:x) (x++[0]) $ [1,2,3]
[1,3,5,3]





share|improve this answer
























  • Thank you! That's very useful. Now I have something to study its meaning.

    – Marco_O
    Nov 20 '18 at 17:50














4












4








4







Basically, all you want to compute component-wise sums between your input list and a shifted version of it:



a   b   c   d   e
a b c d e
---------------------------
a a+b b+c c+d d+e e


Fill each empty slot with a 0 (0:x and x++[0]), and you just need zipWith



> x -> zipWith (+) (0:x) (x++[0]) $ [1,2,3]
[1,3,5,3]





share|improve this answer













Basically, all you want to compute component-wise sums between your input list and a shifted version of it:



a   b   c   d   e
a b c d e
---------------------------
a a+b b+c c+d d+e e


Fill each empty slot with a 0 (0:x and x++[0]), and you just need zipWith



> x -> zipWith (+) (0:x) (x++[0]) $ [1,2,3]
[1,3,5,3]






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 17:48









chepnerchepner

246k32233324




246k32233324













  • Thank you! That's very useful. Now I have something to study its meaning.

    – Marco_O
    Nov 20 '18 at 17:50



















  • Thank you! That's very useful. Now I have something to study its meaning.

    – Marco_O
    Nov 20 '18 at 17:50

















Thank you! That's very useful. Now I have something to study its meaning.

– Marco_O
Nov 20 '18 at 17:50





Thank you! That's very useful. Now I have something to study its meaning.

– Marco_O
Nov 20 '18 at 17:50


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397811%2fhow-can-i-sum-the-middle-elements-of-an-expanding-list-in-haskell%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]