Hi,
do you have any particular reason for using arrays when solving the problem? Arrays have constant size, so when you want to add a new item to an array, you'd have to create a new larger array, copy the elements from the previous array and add the new element.

You could use ResizeArray type, which is just like array, but allows you to add new elements (internally, it keeps an array with some additional "empty" space, so it doesn't have to re-create the array every time). It is an alias for .NET System.Collections.Generic.List<T> type.

1
2
3
4
5
6
7
 
let addstrArray (sArray : ResizeArray<string>, str2Add : string) =
    if sArray |> Seq.exists(fun x -> x = str2Add) then
        printfn "Already there"
    else
        printfn "Can add"
        sArray.Add(str2Add)

However, this is still not very efficient, because you need to search the entire array before adding every single element. A more efficient way would be to use hashtable or functional Map (which can quickly tell you whether an alement already exists in the map). Note that Map is functional, so it returns a new (modified) map when you add an element (the original map remains unchanged):

1
2
3
4
5
6
7
8
9
10
// Create an empty map
let map = Map.empty 

// Add some string & create new map 
// (we add 'foo' as a key and '1' as a value, but we later ignore values)
// When key is already present, it doesn't do anything (just as you wanted)
let map2 = map |> Map.add "foo" 1

// Get array containing all keys
map2 |> Seq.map (fun a -> a.Key) |> Array.ofSeq
By on 1/24/2010 6:22 AM ()

Hi, Tomasp:
Yes, I have the reason for using array.
Because I need the fixed length of array, whenever the array is filled with non-empty string, my program will finish. Therefore, I really need array.
Thank you for your code and explanation.
But if you can provide the array version, that will be even better.
Thanks,

By on 1/24/2010 7:17 AM ()

based on your description, you are using array as a dictionary. why not just use a dictionary ? You can query the lengh of a dictionary any time(which is O(1), just like array.length). In other words, you can check the length of the dictionary after each addition to meet your spec of 'whenever the array is filled', which is just a 'if dictionary.count == my_preset_length'.

so i failed to see why you NEED array.

By on 1/24/2010 10:50 AM ()

Hi, Gary:
I think you are right: using dictionary seems to be a better solution.
I can see that I can not add an existing entry to the dictionary, however, since I don't know dictionary well enough.
Could you show me the code, how I can check and add an entry to a dictionary until the count reaches the pre-defined number?
let aDict = new Dictionary <string, string> ()
I want to add some entries, like: ("A","Apple"), ("B", "Boy"), ("C","Call") until the counts is 3.
Thanks,

By on 1/25/2010 1:43 PM ()
1
2
3
4
let a = new System.Collections.Generic.Dictionary<string,string>()
a.["abc"] <- "efg"
if a.Count == 3 then whatever else whatever
By on 1/25/2010 2:32 PM ()

Hi, Gary:
Thank you for your help. But how I can handle the exception?
Here is my code:

> open System.Collections.Generic

let a = new Dictionary<string,string>()
a.["A"] <- "Apple"

let dictAdd (aDict : Dictionary<string,string>, aKey : string, aValue : string) =
if aDict.Count = 3 then printfn "Quit"
else aDict.Add(aKey, aValue)
aDict

dictAdd(a, "B", "BOY");;

val a : System.Collections.Generic.Dictionary<string,string> =
dict [("A", "Apple"); ("B", "BOY")]
val dictAdd :
System.Collections.Generic.Dictionary<string,string> * string * string ->
System.Collections.Generic.Dictionary<string,string>

> dictAdd(a, "B", "BOY");;
System.ArgumentException: An item with the same key has already been added.
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at FSI_0003.dictAdd(Dictionary`2 aDict, String aKey, String aValue)
at <StartupCode$FSI_0004>.$FSI_0004.main@()
Stopped due to error
>
I want a message telling the entry is already there, not showing the exception.
Thanks,

By on 1/25/2010 11:17 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