[Question]
  [
Given some finite list, return a list of all its prefixes, including an empty list, in ascending order of their length.
(Basically implementing the Haskell function [`inits`](http://hackage.haskell.org/package/base-4.11.1.0/docs/Data-List.html#v:inits).)
### Details
* The input list contains numbers (or another type if more convenient).
* The output must be a *list of lists*.
* The submission can, but does not have to be a function, any [default I/O](https://codegolf.meta.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods) can be used.
* There is a [CW answer](https://codegolf.stackexchange.com/a/177147/24877) for all [trivial solutions](https://codegolf.meta.stackexchange.com/questions/11211/should-we-combine-answers-where-the-same-code-works-in-many-different-languages/11218#11218).
* This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest code in bytes wins
### Example
```
[] -> [[]]
[42] -> [[],[42]]
[1,2,3,4] -> [[], [1], [1,2], [1,2,3], [1,2,3,4]]
[4,3,2,1] -> [[], [4], [4,3], [4,3,2], [4,3,2,1]]
```
  
[Answer]
# [Haskell](https://www.haskell.org/), 20 bytes
Edit: Yet a byte shorter with a completely different scan.
An anonymous function slightly beating the trivial import.
```
scanr(\_->init)=<<id
```
[Try it online!](https://tio.run/##HcqxCsIwEADQvd/hkEIasHYqTRcnt4KLkAY50iQ9TE9JIv69sbi86a2QHjaEsgGSRMo2gsmHNwUkm8QGL5bW50c4waKFpe@vOSL5ZlSX/XobdV2L/62cnEsyQJHN92ZEwlzLYcClFKUr1bU7R97yE@/017gAPpXmdp6mHw "Haskell – Try It Online")
* Uses `=<<` for the abbreviation `(scanr(\_->init)=<<id) l = scanr(\_->init) l l`.
* Scans a list `l` from right to left, collecting intermediate results with the function `\_->init`.
* That function ignores the elements scanned through (they're only used to get the right total length for the collected results), so really iterates applying `init` to the initial value of the scan, which is also `l`.
[Answer]
# [brainfuck](https://github.com/TryItOnline/brainfuck), ~~21~~ 12 bytes
*-9 bytes thanks to Arnauld suggesting the separator `ÿ` instead of newlines*
```
-[[<]>[.>],]
```
[Try it online!](https://tio.run/##SypKzMxLK03O/v9fNzraJtYuWs8uVif2//@AotS0zIrUYgA "brainfuck – Try It Online")
Takes bytes through STDIN with no null bytes and prints a series of prefixes separated by the `ÿ` character with a leading `ÿ` character. For example, for the input `Prefixes`, the output is `ÿÿPÿPrÿPreÿPrefÿPrefiÿPrefixÿPrefixeÿPrefixes`.
For readability, [here's a version with newlines instead](https://tio.run/##SypKzMxLK03O/v9fGw6io21i7aL17GJ1Yv//DyhKTcusSC0GAA).
# Explanation:
```
-              Create a ÿ character in cell 0
 [        ,]   While input, starting with the ÿ
  [<]>           Go to the start of the string
      [.>]       Print the string
          ,      Append the input to the end of the string
```
[Answer]
# JavaScript (ES6), 33 bytes
```
a=>[b=[],...a.map(n=>b=[...b,n])]
```
[Try it online!](https://tio.run/##bco7DoAgEEXR3pVAMk4i0g4bIVOM32gQiBq3j9TG8p13d3nkGs8t321M01wWKkLOD@QZEFHwkKwiuQp1DhBZcxlTvFKYMaRVLcqz1s2HrPnBDgz0YP/y6ga6@pQX "JavaScript (Node.js) – Try It Online")
### How?
```
+--- a = input array
|
|       +--- initialize b to an empty array and include it as the first entry
|       |    of the output (whatever the input is)
|       |
|       |          +--- for each value n in a[]:
|       |          |
|       |          |        +--- append n to b[] and include this new array in
|       |          |        |    the final output
|       |          |        |
a => [b = [], ...a.map(n => b = [...b, n])]
               |                  |
               +---------+--------+
                         |
      spread syntax: expands all elements of
      the child array within the parent array
```
[Answer]
# CW for all [trivial entries](https://codegolf.meta.stackexchange.com/questions/11211/should-we-combine-answers-where-the-same-code-works-in-many-different-languages/11218#11218)
# [Clean](https://github.com/Ourous/curated-clean-linux), 19 bytes
Haskell version works in Clean too.
```
import StdLib
inits
```
[Try it online!](https://tio.run/##S85JTcz7n5ufUpqTqpCbmJn3PzO3IL@oRCG4JMUnM@l/cEkikGOrkJmXWVKsEG2oo2Cko2Cso2Cio2Cqo2AW@/9fclpOYnrxf11Pn/8ulXmJuZnJEA7EAAA "Clean – Try It Online")
# [Haskell](https://www.haskell.org/), 22 bytes
```
import Data.List
inits
```
[Try it online!](https://tio.run/##y0gszk7Nyfn//39mbkF@UYmCS2JJop5PZnEJV25iZp6CrUJBUWZeiYKKQmZeZkmxQrShnp5p7H8A "Haskell – Try It Online")
# [Prolog (SWI)](http://www.swi-prolog.org), 6 bytes
```
prefix
```
[Try it online!](https://tio.run/##KyjKz8lP1y0uz/z/3163oCg1LbNCI1In2lDHSMdYx0THNFZTp7wosyQ1J08jUlPH0NbA2tDWUO//fwA "Prolog (SWI) – Try It Online")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 3 bytes
```
ṭṖƤ
```
[Try it online!](https://tio.run/##y0rNyan8///hzrUPd047tuT/4fZHTWsUjk56uHMGiBH5/390rI5CtIkRiDTUUTDSUTDWUTCJBQA "Jelly – Try It Online")
### How it works
```
ṭṖƤ  Main link. Argument: A
  Ƥ  Map the link to the left over all non-empty(!) prefixes of A.
 Ṗ       Pop; remove the last element.
ṭ    Tack; append A to the resulting list.
```
[Answer]
# [R](https://www.r-project.org/), ~~40~~ 39 bytes
```
function(L)lapply(0:length(L),head,x=L)
```
[Try it online!](https://tio.run/##K/qfpmCj@z@tNC@5JDM/T8NHMyexoCCnUsPAKic1L70kAyiik5GamKJTYeuj@T9NIyezuERDU5MLyjIxQrANdYx0jHVMNDX/AwA "R – Try It Online")
*-1 byte thanks to [digEmAll](https://codegolf.stackexchange.com/users/41725/digemall)*
The output of R's `list` type is a bit weird; it uses sequential indexing, so for instance, the output for
`list(1,2)` is 
```
[[1]]                     # first list element
list()
[[2]]                     # second list element
[[2]][[1]]                # first element of second list element
[1] 1
[[3]]                     # third list element
[[3]][[1]]                # first element of third list element
[1] 1
[[3]][[2]]                # etc.
[1] 2
```
[Taking input as a vector instead](https://tio.run/##K/qfpmCj@z@tNC@5JDM/T8NHMyexoCCnUsPAKic1L70kAyiik5GamKJTYeuj@T9NI680N7UoM1nDQFOTK00jWcPECMow1DHSMdYx0dT8DwA) gives a neater output format, although then the inputs are not technically `list`s.
[Answer]
# [Python 2](https://docs.python.org/2/), 32 bytes
```
f=lambda l:(l and f(l[:-1]))+[l]
```
[Try it online!](https://tio.run/##K6gsycjPM/r/P802JzE3KSVRIcdKI0chMS9FIU0jJ9pK1zBWU1M7Oif2f0FRZl4JUDA6VpMLzjYxQuYZ6hjpGOuYxGr@BwA "Python 2 – Try It Online")
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 4 bytes
```
²£¯Y
```
[Try it online!](https://ethproductions.github.io/japt/?v=1.4.6&code=sqOvWQ==&input=WzEsMiwzLDRdCi1R)
Explanation:
```
²       :Add an arbitrary extra item to the end of the array
 £      :For each item in the new array:
  ¯Y    : Get an array of the items that are before it
```
[Answer]
# [Perl 6](http://perl6.org/), 13 bytes
```
{(),|[\,] @_}
```
[Try it online!](https://tio.run/##K0gtyjH7n1upoJamYPu/WkNTpyY6RidWwSG@9n9xYqWCUrVeemZxSa2CrZ1CtUKahkq8JlhAoVZJIS2/SAGoQcHESM8HKKSjoGGoo2Cko2AMFAIKa5iAmUABQ03r/wA "Perl 6 – Try It Online")
To explain:
In Perl 6 you can wrap an operator in square brackets as an alternate way to write a list reduction. `[+] @array` returns the sum of the elements in `@array`, `[*] @array` returns the product, etc. You can also precede the operator with a backslash to make a "triangular" reduction, which some languages call "scan." So `[\+] @array` returns a list consisting of the first element of `@array`, then the sum of the first two elements, then the sum of the first three elements, etc.
Here `[\,] @_` is a triangular reduction over the input array `@_` using the list construction operator `,`. So it evaluates to a lists of lists: the first element of `@_`, the first two elements of `@_`, etc. That's almost what's needed, but the problem calls for a single empty list first. So the first element of the return list is a literal empty list `(),`, then the reduction over the input list is flattened into the rest of the return list with `|`.
[Answer]
# JavaScript, 36 bytes
```
a=>[...a,0].map((x,y)=>a.slice(0,y))
```
[Try it online!](https://tio.run/##DclLCoAgEADQ64wwDfbZ6kWkxWAahqlkRJ3eXD7ewQ9Xe4VyDylvrnnVWGlDRIxypZMLwIufUJqpxmAdyC7RbE41R0cx7@DBLDjjhOPa5wc "JavaScript (Node.js) – Try It Online")
[Answer]
# Mathematica, ~~22~~ 21 bytes
*-1 byte thanks to [Misha Lavrov](https://codegolf.stackexchange.com/users/74672)!*
```
{}~FoldList@Append~#&
```
Pure function. Takes a list as input and returns a list of lists as output. I believe this is the shortest possible solution.
[Answer]
# [Husk](https://github.com/barbuz/Husk), 2 bytes
```
Θḣ
```
Gets all the `ḣ`eads and then prepends `Θ` (in this case `[]`):
[Try it online!](https://tio.run/##yygtzv7//9yMhzsW////P9pQx0jHWMckFgA "Husk – Try It Online")
(needs type annotation for empty list: [Try it online!](https://tio.run/##yygtzv7//9yMhzsW////PzrWyscPAA "Husk – Try It Online"))
[Answer]
# [J](http://jsoftware.com/), 5 bytes
```
a:,<\
```
[Try it online!](https://tio.run/##y/r/P83WSiHRSscm5n9qcka@QpqCekFRalpmRWqxOhdUxFDBVMHsPwA "J – Try It Online")
[Answer]
# [PowerShell](https://github.com/TryItOnline/TioSetup/wiki/Powershell), 65 bytes
```
param($a)'';$x=,0*($y=$a.count);0..--$y|%{$x[$_]=@($a[0..$_])};$x
```
[Try it online!](https://tio.run/##K8gvTy0qzkjNyfn/vyCxKDFXQyVRU13dWqXCVsdAS0Ol0lYlUS85vzSvRNPaQE9PV1elska1WqUiWiU@1tYBqDgaKApka9YCtfz//99Bw1DHSMdYx0QTAA "PowerShell – Try It Online")
PowerShell *helpfully* unrolls lists-of-lists when the default `Write-Output` happens at program completion, so you get one item per line. Tack on a [`-join','`](https://tio.run/##FYttC0AwFEb/zNU23S1v37TyPyQtKYRpCOG3X/Pt6ZzzLPZo3dq140i0GGcmDkYwlsOpMQo5XBqMauw@byKPlJISrie44SyhrnTh49JTv8XrL7@p5WD7mSF7iajgMSaYYiY@ "PowerShell – Try It Online") to see the list-of-lists better, by converting the inner lists into strings.
(Ab)uses the fact that attempting to output an empty array (e.g., `@()`) results in no output, so an empty array input just has `''` as the output, since the `$a[0..$_]` will result in nothing. It will also throw out some [spectacular error messages.](https://tio.run/##K8gvTy0qzkjNyfn/vyCxKDFXQyVRU13dWqXCVsdAS0Ol0lYlUS85vzSvRNPaQE9PV1elska1WqUiWiU@1tYBqDgaKApka9YCtfz//99BQxMA "PowerShell – Try It Online")
[Answer]
# [Common Lisp](http://www.clisp.org/), 39 bytes
```
(defun f(l)`(,@(if l(f(butlast l))),l))
```
[Try it online!](https://tio.run/##Dck7CsAgEAXAq7wub8Emvz5HyccsCItI1PNvbKaZx1It7oyv9gylyclwMCmMyrs3u2qDiUgYOMuXcgMVE2csWLFhH@k/ "Common Lisp – Try It Online")
### Explanation
```
(defun f(l)                           )  ; Define a function f
           `(                        )   ; With the list (essentially capable of interpolation), containing:
             ,@                          ;     The value of, flattened to one level
               (if l              )      ;         If l is not the empty list (which is the representation of nil, i.e. the only falsy value)
                    (f(butlast l))       ;         Recurse with all of l but the tail
                                   ,l    ;     The value of l
```
[Answer]
## F#, 53 bytes
I've actually got two pretty similar answers for this, both the same length. They both take a generic sequence `s` as a parameter.
First solution:
```
let i s=Seq.init(Seq.length s+1)(fun n->Seq.take n s)
```
[Try it online!](https://tio.run/##TY1BC4JAFITv/oqHEChhZHUzFzx0DzqKh0VWe7i9rd1XIPTfN9cinNPMNzDTuaw1VnmvFQOCKy/qsUFCToLRinq@glvnadI9CSgTAbMcFBC41NfHE7EdzwaJRROFkZtEAmn7F5QRTAqsNVqrltHQDwbVh2Jf7Iq8@ZO3AFyG@ctU1soxmvndTj8dQbyq4sXot9z6Dw)
`Seq.take` takes the first `n` elements of the sequence. `Seq.init` creates a new sequence with a count (in this case) of the length of sequence `s` plus 1, and for each element in the sequence takes the first `n` elements in `s`.
Second solution:
```
let i s=Seq.map(fun n->Seq.take n s){0..Seq.length s}
```
Similar to before, except it creates a sequence from 0 to the length of `s`. Then takes that number of elements from `s`.
[Try this online too!](https://tio.run/##TY09C4MwFEV3f8VDKLSDwX5s1oBD90JHcQgSbWh80Ze0IG1/e2psKd7tnAv3NjapDUnvtXSgwOYXObBO9OvmjoAJD@jETQKC3TxTxoLQElt3Bfv25fGEjsazUeh4FYWRTigEQe0D8gimBFcbrWXtlMGfDCkP2T7bZdvqb14c1BLmc1MQiTGafU/TT4MQr4p4MfotU/8B)
[Answer]
# [SWI PROLOG](http://www.swi-prolog.org) 22 bytes
`i(X,Y):-append(X,_,Y).`
[Answer]
# [Ruby](https://www.ruby-lang.org/), ~~31~~ 29 bytes
```
->a{[a*i=0]+a.map{a[0,i+=1]}}
```
[Try it online!](https://tio.run/##KypNqvyfZvtf1y6xOjpRK9PWIFY7US83saA6MdpAJ1Pb1jC2tvZ/gUKanoZCdKwCFGgq2NoqREfHxnJBZUyMYlFldEBCcGlDHSMdYx2TWIQ0UAxM6BhBKR1jOAOoEGEwkGukY4ii0wRMQDSA5eEMoMLY/wA "Ruby – Try It Online")
Explanation:
```
->a{             # take array input a
  [a*i=0]+       # set i to 0 and add whatever comes next to [[]] (a*0 == [])
  a.map{         # for every element in a (basically do a.length times)
    a[0,i+=1]  # increment i and return the first i-1 elements of a to map
  }
}
```
[Answer]
# [K (ngn/k)](https://bitbucket.org/ngn/k), ~~8~~ 7 bytes
```
1_',\0,
```
[Try it online!](https://tio.run/##y9bNS8/7/z/NyjBeXSfGQOd/iVW1SnRlXZFVmkKFdUJ@trVGQlpiZo51hXWldZFmbC1XSbSigbWOokEskKVjYmStAeKaGGmC@IYKRgrGCiYQMUNrINcaLGQNldCM/Q8A "K (ngn/k) – Try It Online")
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), ~~9~~ 8 bytes
```
{∧Ė|a₀}ᶠ
```
[Try it online!](https://tio.run/##SypKTM6ozMlPN/r/v/pRx/Ij02oSHzU11D7ctuD//2hDHSMdYx2T2P9RAA "Brachylog – Try It Online")
### Explanation
```
{     }ᶠ    Find all:
 ∧Ė          The empty list
   |         Or
    a₀       A prefix of the input
```
[Answer]
# MATL, ~~15~~ 12 bytes
*3 bytes saved thanks to @Giuseppe*
```
vin:"G@:)]Xh
```
Try it at [MATL Online](https://matl.io/?code=vin%3A%22G%40%3A%29%5DXh&inputs=%5B1%2C2%2C3%2C4%5D&version=20.10.0).
Due to the way that MATL displays the output, you can't explicitly see the empty array in the cell array. [Here](https://matl.suever.net/?code=vin%3A%22G%40%3A%29%5DXh%26D&inputs=%5B1%2C2%2C3%2C4%5D&version=20.10.0) is a version that shows the output a little more explicitly.
**Explanation**
```
v       # Vertically concatenate the (empty) stack to create the array []
i       # Explicitly grab the input
n       # Compute the number of elements in the input (N)
:       # Create an array from [1, ..., N]
"       # Loop through this array
  G     # For each of these numbers, M
  @:    # Create an array from [1, ..., M]
  )     # Use this to index into the initial array
]       # End of the for loop
Xh      # Concatenate the entire stack into a cell array
```
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 6 bytes
```
Ｅθ…θκθ
```
[Try it online!](https://tio.run/##S85ILErOT8z5/z@gKDOvRMM3sUCjUEfBuTI5J9U5Ix/MydbU1LTmgsgXalr//x8drWSopKOgZAQijEGEiVJs7H/dshwA "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
 θ      Input array
Ｅ       Map over elements
   θ    Input array
  …     Moulded to length
    κ   Current loop index
        Implicitly print each array double-spaced
     θ  Input array
        Implicitly print
```
It's possible at a cost of 1 byte to ask Charcoal to print an `n+1`-element array which includes the input as its last element, but the output is the same, although the cursor position would be different if you then went on to print something else.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 3 bytes
```
η¯š
```
Explanation:
```
η    Prefixes
  š  Prepend
 ¯   Global array (empty by default)
```
[Try it online!](https://tio.run/##yy9OTMpM/f//3PZD648u/P8/2sQoFgA "05AB1E – Try It Online")
[Answer]
# [RAD](https://bitbucket.org/zacharyjtaylor/rad), 7 bytes
```
(⊂⍬),,\
```
[Try it online!](https://tio.run/##K0pM@f9f41FX06PeNZo6OjH///83VDBSMFYwAQA "RAD – Try It Online")
This also works in Dyalog APL as a function.
## How?
This works the same for both APL and RAD, given their close relation.
* `(⊂⍬)` the empty array
* `,` prepended to
* `,\` the prefixes (which exclude the empty array.)
[Answer]
# [Groovy](http://groovy-lang.org/), 37 bytes
```
{x->(0..x.size()).collect{x[0..<it]}}
```
[Try it online!](https://tio.run/##Sy/Kzy@r/F9iG22oY6RjrGMSy1Vp@7@6QtdOw0BPr0KvOLMqVUNTUy85PycnNbmkuiIaKGyTWRJbW/u/oCgzryQnT6FSo0TzPwA "Groovy – Try It Online")
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 5 bytes
```
Êò@¯Y
```
[Try it online!](https://tio.run/##y0osKPn//3DX4U0Oh9ZH/v8fbahjpGOsYxLLxaUbCAA "Japt – Try It Online")
[Answer]
# [brainfuck](https://github.com/TryItOnline/brainfuck), 43 bytes
Take a list of non-null characters as input and returns all prefixes separated by newline. Requires double-infinite or wrapping tape.
```
,>-[+>,]<[-<]<<++++++++++[[<]>[.>]>[-<+>]<]
```
[Try it online!](https://tio.run/##SypKzMxLK03O/v9fx043WttOJ9YmWtcm1sZGGw6io21i7aL17ICEro22XaxN7P//AUWpaZkVqcUA "brainfuck – Try It Online")
[Answer]
# Powershell, 24 bytes
```
,@()
$args|%{,($x+=,$_)}
```
Explanation:
1. The trivial accumulator wrapped to array by unary `,`.
2. The script uses [splatting](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting). See parameter `@a`.
Test script:
```
$f = {
,@()
$args|%{,($x+=,$_)}
}
@(
    ,( @()        , @() )
    ,( @(42)      , @(@(),@(42)) )
    ,( @(1,2,3,4) , @(@(), @(1), @(1,2), @(1,2,3), @(1,2,3,4)) )
    ,( @(4,3,2,1) , @(@(), @(4), @(4,3), @(4,3,2), @(4,3,2,1)) )
) | % {
    $a,$expected = $_
    $result = &$f @a
    $false -notin $(
        $result -is [array]
        $result.Count -eq $expected.Count
        for($j=0; $j-lt$result.Count; $j++){
            "$($result[$j])" -eq "$($expected[$j])"
        }
    )
    # $result   # uncomment this line to display a result
}
```
Output:
```
True
True
True
True
```
[Answer]
# [C# (Visual C# Interactive Compiler)](http://www.mono-project.com/docs/about-mono/releases/5.0.0/#csc), 39 bytes
```
x=>x.Select((_,i)=>x.Take(i)).Append(x)
```
[Try it online!](https://tio.run/##bY/LCsIwEEX3@YqQVYJjwepOW3ChoLir4KIUqXGqQY2SplqQfnuNL9TiZuCeucNhZN6WuarHhZaDyUgXBzTpao8DpW0I36C5DEOa0aAug7D0ItyjtJwvQYl7nqc75EoIb3g6oV7zUtR9QtxVnMQJtZjbnAZU44W@GLkS@snXCn5iz2@ADvjQhV6z5pgPHUcrZyPZ0WAqt/ycmofStZ5q8bAtjLI4Uxo5i1krskbpjTc9Ks0ZMMj4vSren7kv/7VK0WIJE8/ZJ1V9Aw "C# (Visual C# Interactive Compiler) – Try It Online")
[Answer]
# [F# (Mono)](http://www.mono-project.com/), 45 bytes
```
fun x->List.mapi(fun i y->List.take i x)x@[x]
```
[Try it online!](https://tio.run/##SyvWzc3Py/@fk1qikKZgq6DxP600T6FC184ns7hELzexIFMDJJCpUAkVKknMTgVyKzQrHKIrYv9rWltzcRUUZeaVpOUpKKk6KilopEXHamIImRhhETS0NrI2tjbBphwobmRtGKv5HwA "F# (Mono) – Try It Online")
I am not totally sure if this is valid, but it seems like it follows the same "anonymous lambda" syntax that I've seem used in several other languages.
]