Thursday, October 28, 2010

Studying F# : Difference of List, Array and Tuple

Now I'm studying about F#. And I learned the difference of List, Array and Tuple.

List

A list in F# is an ordered, immutable series of elements of the same type.
Each elements are separated by semicolons.
> let integer_list = [1; 2; 3];;

val integer_list : int list = [1; 2; 3]

> let string_list = ["a"; "b"; "c"];;

val string_list : string list = ["a"; "b"; "c"]

Array

Arrays are fixed-size, zero-based, mutable collections of consecutive data elements that are all of the same type.
This notation is similar to List, but with vertical bar.
> let int_array = [|1; 2; 3|];;

val int_array : int [] = [|1; 2; 3|]

> let string_array = [|"a"; "b"; "c"|];;

val string_array : string [] = [|"a"; "b"; "c"|]

> Array.fill string_array 0 2 "x";;
val it : unit = ()
> string_array;;
val it : string [] = [|"x"; "x"; "c"|]

Tuple

A tuple is a grouping of unnamed but ordered values, possibly of different types.
There are elements separated by comma.
> let integer_tuple = (1, 2, 3);;

val integer_tuple : int * int * int = (1, 2, 3)

> let string_tuple = ("1", "2", "3");;

val string_tuple : string * string * string = ("1", "2", "3")

> let variable_tuple = (1, "two", 3.0);;

val variable_tuple : int * string * float = (1, "two", 3.0)

No comments:

Post a Comment