I am trying to write a Haskell function which takes a list ls and returns all sub-lists obtained by removing one element from ls. An additional constraint is that the returned list of lists must be in the order of the missing element.

Here is what I have. I know there must be a simpler solution.

subOneLists :: [a] -> [[a]]
subOneLists ls = let  helper :: [a] -> [a] -> [[a]] -> [[a]]
                      helper _ [] ss = ss
                      helper ps (x:xs) ss = helper ps' xs ss'
                        where ps' = ps ++ [x]
                              ss' = ss ++ [ps ++ xs]
                 in helper [] ls []

λ> subOneLists [1, 2, 3, 4]

3 Answers 3


Here's a simpler way to implement it:

subOneLists :: [a] -> [[a]]
subOneLists [] = []
subOneLists (x:xs) = xs : map (x :) (subOneLists xs)
  • \$\begingroup\$ This is really clever \$\endgroup\$
    – Paul
    Commented Nov 14, 2018 at 4:10
  • 3
    \$\begingroup\$ this repeats the old inits bug which makes (last $ subOneLists xs !! n) quadratic in n. My version as well as the newer, corrected version of inits in the library makes it linear. \$\endgroup\$
    – Will Ness
    Commented Nov 14, 2018 at 13:43

Look out for standard functions that can help you!

Prelude Data.List> let subOneLists ls = zipWith (++) (inits ls) (tail $ tails ls)
Prelude Data.List> subOneLists [1, 2, 3, 4]

This uses the fact that the inits- and tails-elements at corresponding index always recombine to the original list, but with a variably splitting point:

Prelude Data.List> let ls = [0..7] in mapM_ print (zip (inits ls) (tails ls))

If you now “shift up” that tails, by dropping its head, you effectively lose the head of each of the contained lists:

Prelude Data.List> let ls = [0..7] in mapM_ print (zip (inits ls) (tail $ tails ls))

And that can just be ++ combined with the inits again.

  • \$\begingroup\$ makes me wonder that maybe there should be revinits in the library somewhere... \$\endgroup\$
    – Will Ness
    Commented Nov 14, 2018 at 13:28
  • \$\begingroup\$ Would probably make more sense to make that a rewrite rule for tails . reverse, if even necessary. \$\endgroup\$ Commented Nov 14, 2018 at 13:44
  • \$\begingroup\$ I meant reversed_inits [1..] !! 10 == [10,9..1]. :) (i.e. map reverse . inits but linear) \$\endgroup\$
    – Will Ness
    Commented Nov 14, 2018 at 13:46
  • \$\begingroup\$ Ok, reverse . tails . reverse... yeah, that's ah bit meh. Still – I don't think it's good to pack base with every combination of reversal and disassembly. If you use any of these, then it's probably not optimal for performance anyway (compared to something Data.Vector based), and if performance isn't that critical then just combine simple list functions. \$\endgroup\$ Commented Nov 14, 2018 at 13:51
  • 1
    \$\begingroup\$ see my updated comment. It's supposed to be linear, that's the point. and work for infinite inputs too. \$\endgroup\$
    – Will Ness
    Commented Nov 14, 2018 at 13:52

List as an abstract concept can have many representations.

In particular, with a list being represented by its "zipper" - a pairing of a reversed prefix and a suffix, it becomes possible to have a linear solution to this problem, as opposed to the quadratic one which is unavoidable with the plain linear representation :

picks :: [a] -> [([a], [a])]
picks []     = []
picks (x:xs) = go [x] xs
   go pref suff@(x:xs) = (pref,suff) : go (x:pref) xs
   go pref []          = [(pref,[])]

Using this, your problem becomes

foo = map (\(a,b) -> revappend (tail a) b) . picks

revappend a b = foldl (flip (:)) b a

This is of course again quadratic, but maybe you could keep the prefixes reversed, to stay linear:

import Control.Arrow (first)

foo' = map (first tail) . picks               -- or,
 --  = map (\(x,y) -> (tail x, y)) . picks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.