# Fold a List in Half

We are going to fold a list of integers. The procedure to do so is as follows, If the list is of even length, make a list of half of its length where the nth item of the new list is the sum of the nth item of the old list and the nth-to-last item of the old list. For example if we had the list

[1 2 3 4 5 6 7 8]


We would fold it like so

 [8 7 6 5]
+[1 2 3 4]
__________
[9 9 9 9]


If the list is of odd length, to fold it we first remove the middle item, fold it as if it were even and the append the middle item to the result.

For example if we had the list

[1 2 3 4 5 6 7]


We would fold it like so

 [7 6 5]
+[1 2 3]
__________
[8 8 8]
++     [4]
__________
[8 8 8 4]


Write a program or function that takes a list of integers as input and outputs that list folded.

This is a question so answers will be scored in bytes, with fewer bytes being better.

## Sample implementation

Here's an implementation in Haskell that defines a function f that performs a fold.

f(a:b@(_:_))=a+last b:f(init b)
f x=x


Try it online!

• When you say integers, does this include zero or negative integers?
– Neil
Jul 31 '17 at 19:53
• @Neil Yes it does. Jul 31 '17 at 19:54
• @GrzegorzPuławski You should not sort the list. Any ordered collection is allowed, e.g. vector or array. Jul 31 '17 at 20:51
• @DavidStarkey Most reasonable lists will not overflow with a reasonable amount of memory. Folding doesn't actually increase the sum so lists will converge to a singleton of the sum of the original list. Aug 1 '17 at 14:05
• @WheatWizard I don't know about that, I've heard it's impossible to fold any list in half more than 7 times. Aug 2 '17 at 4:23

# Perl 6, 28 bytes

{.[^*/2]Z+.[$_-1...$_/2,$_]}  Try it online! ### Explanation: { } # Anonymous code block .[^*/2] # The first half of the list Z+ # Zip added to .[$_-1...$_/2 # The other half of the list ,$_]    # And zero for the middle element


# C# (Visual C# Interactive Compiler), 82 bytes

x=>{int i=0,l=x.Count;for(;i<l/2;)x[i]+=x[l-++i];return x.Take(l/2+l%2).ToList();}


Try it online!

# Ruby, 34 bytes

f=->l{l[1]?[l.shift+l.pop]+f[l]:l}


Try it online!

# Zsh, 55 bytes

If the answer must be put to stdout, but output whitespace is flexible, 55 bytes:

for ((;++i<=#/2;))<<<$[$@[i]+$@[-i]] <<<$@[#%2*(1+#/2)]


Try it online!

If the output must be stored in an array, 53 bytes: (We can't use this method in place of the above because of <<<...[--i].... The here-string forces a subshell, so the decremented value of i never makes it out.)

for n ($@[1,#/2])y+=($[$@[--i]+n]) y+=$@[#%2*(1+#/2)]


Try it online!

If the answer must be output in one line separated by spaces, then append <<<$y for a 6 byte penalty. Zsh arrays are indexed from the start starting at 1, or from the end starting at -1. So what happens if you attempt to index at 0? Well, nothing! We take advantage of that here to only output the middle number based on a parity check: $@[#%2*(1+#/2)]
#      #       # parameter count
(1+#/2)    # index of the middle element when count is odd
#%2*(1+#/2)    # multiply by 0 if even, or 1 if odd
\$@[           ]   # Index the parameter array


# C (gcc), 55 bytes

f(_,l)int*_;{for(;l--;printf("%d ",l?*_+_++[l--]:*_));}


Try it online!