[Haskell] [Oz] Oz function that insert element to all possible positions in a list

function that insert X to all possible positions in the list L.

Mozart Oz Programming Language

declare
fun {AllInsert X L}
   IntFrom in
   fun lazy {IntFrom K} K|{IntFrom K+1} end
   {Map {List.take {IntFrom 0} {List.length L}+1}
    fun {$ Pos}
       {FoldR L (fun {$ Z Y}
      case Y of nil then nil
      [] S#I then
         if I \= Pos-1 then
     {Append [Z] S}#(1+I)
         else
     {Append [X Z] S}#(1+I)
         end
      end
   end) ({List.drop [X] Pos}#0)
       }.1
    end}
end

{Browse {AllInsert 10 [a b c d]}} %shows [a b c d 10] [a b c 10 d]
                                %[a b 10 c d] [a 10 b c d]
                                %[10 a b c d]]


Haskell Programming Language

-- function that inserts an element at all possible positions in a list

allInsert :: a -> [a] -> [[a]]
allInsert x [] = [[x]]
allInsert x xs = map f [0..(length xs)]
 where f s = insert x s xs
-- allInsert "x" ["a","b","c","d","e"]  

Comments

Popular posts from this blog

[Azure Websites PHP] Cross Domain request results in blank response page after Preflight HTTP OPTIONS

[Magento] Create Contact Form with Dynamic Recipient