1
2
3
4
5
6
7
8
9
let removeElementAt index myArr = 

    let temp = [|  for i = 0 to Array.length myArr - 1 do 
                       if i <> index then yield myArr.[ i ]
                |]
    temp 

 
By on 1/10/2010 2:18 PM ()

@marekb: Note that, unlike Joh's solution, your code (or the ResizeArray inside of toArray, that is) will create temporary arrays proportional to log(n).

By on 1/11/2010 2:55 AM ()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
> let f n arr = Array.init (Array.length arr - 1) (fun i -> if i < n-1 then arr.[ i ] else arr.[i+1]);;

val f : int -> 'a [] -> 'a []

> f 3 [|1..10|];;


val it : int [] = [|1; 2; 4; 5; 6; 7; 8; 9; 10|]


> f 10 [|1..10|];;


val it : int [] = [|1; 2; 3; 4; 5; 6; 7; 8; 9|]

Edit: My code starts counting at 1, whereas your example starts counting at 0. I'm too lazy to fix my code and test it again...

By on 1/6/2010 2:08 AM ()

Hi, Joh:
Thank you very much for your code. It works.
However, I need the 0-based array, as my program already used 0-based array, it is not easy to change it.
By the way, look at your code, it is also difficult to change it to 0-based array.
Can you show me the code on 0-based array.
Thanks.
By the way, thank you Gary for your code, but somehow, it didn't work in my F#. But, thanks any way!

By on 1/6/2010 10:58 AM ()

zydjohn, what about this?

let f n arr = Array.init (Array.length arr - 1) (fun i -> if i < n then arr.[ i ] else arr.[i+1])

By on 1/8/2010 12:32 PM ()

Hi, Joh:
Thank you very much, your code works.
However, I am a little confused.
How do you explain why this one is 0-based and another one is 1-based?
Thanks and have a nice weekend.

By on 1/8/2010 1:22 PM ()

iterative style does make it a bit difficult for changing the 0/1 based and in general harder to refactor.

following is a generalized version which works for Array/List/LazyList/Seq(with minor modification as F# doesn't have type class) but can handle things of not just 0-based index but also multiple positions for example.

let f p a = Array.zip [|0..(Array.length a - 1)|] a |> Array.filter (p>>not) |> Array.map snd

let del_pos n (pos,_) = n = pos

let del_mpos ns (pos,_) = Set.contains pos ns

f (del_pos 2) [|0..99|];;

f (del_mpos (Set.ofList [2;3])) [|0..99|];;

By on 1/6/2010 1:41 PM ()

let f n = Array.zip [|1..100|] >> Array.filter (fun x -> fst x <> n) >> Array.map snd

f 3 [1..2..200]

By on 1/5/2010 11:18 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper