Search code examples
haskellsublist

Sublists in order in haskell


All I wanna do is really simple but it seems hard for me to implement. I want a function in haskell with this behavior.

orderedsubs [2,5,3,4] = [[],[2],[2,5],[2,5,3],[2,5,3,4]]

Initially I thought about removing last element and put it to the list and repeat the process with the previously created list but once it's stored, it's gone. I don't care if the order of the sublists is different, but I want these specific sublists. So, any ideas? Thanks in advance.


Solution

  • Check inits in Data.List:

    The inits function returns all initial segments of the argument, shortest first. For example,

    inits "abc" == ["","a","ab","abc"]
    

    Note that inits has the following strictness property: inits _|_ = [] : _|_