functional programming - Trying to improve a current ugly piece of code in haskell dealing with lists -
i trying implement function in haskell that'll take arbitrary integer list xs
, integer k
, , returns set of lists k
in possible positions.
for example, xs = [0, 1]
, k = 2
, we'd have
myfunction [0, 1] 2 = [ [2, 0, 1], [0, 2, 1], [0, 1, 2] ]
i've implemented
puton xs x = (take xs) ++ (x:(drop xs)) putonall xs x = map (puton xs x) [0..(length xs)]
yet, feel there must other smarter ways achieve same. code seems trying kill bug missile. make sugestions on ways clever bit of code?
thanks
taken this question:
ins x [] = [[x]] ins x (y:ys) = (x:y:ys):[ y:res | res <- ins x ys]
Comments
Post a Comment