Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Haskell Help

Name: Anonymous 2009-06-23 21:22

Okay, major help needed if someone out there has the time, this has been killing my brain for ages.

-- e.g., insertions 'c' "ab" = ["cab", "acb", "abc"]
-- It may help to define two functions which call each other.

Thus far I have.

insertions :: x -> [x] -> [[x]]
insertions x [] = [[]]
insertions x (y:ys) = ??

Name: Anonymous 2009-06-23 21:38

So, you want to insert 'c' into 'ab' at all possible positions, and return a list of those?


import Data.List (splitAt)

insertions :: a -> [a] -> [[a]]
insertions _ [] = []
insertions a as =
  let asrep = replicate (length as + 1) as
      zipped = zip [0..] asrep
      insert (n, l) = let (f, s) = splitAt n l in f ++ [a] ++ s
      inserted = map insert zipped
  in inserted

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List