Lists in F#

Lists typically play an important role in functional programming and F# is no different. Lists are simply a typed collection of values. By "typed" I mean that the list can only contain entities of the list's type. Type the following code into the F# Interactive:

> let list = [1;2;3];;
val list : int list

The Interactive shows us that our list value has the type of int. Now lets try and create a string list and concatenate our two lists: using the @ operator:

> let list2 = ["a";"b";"c"];;
val list2 : string list

> let list3 = list @ list2;;
stdin(15,19): error: FS0001: Type mismatch

As you can see we get a type mismatch here. This is to be expected since lists are typed and that operation is trying to concatenate lists of two different types. Here's a successful concatenation with string lists:

> let list1 = ["a";"b";"c"] @ ["d";"e";"f"];;
val list1 : string list
>list1;;
val it : string list = ["a"; "b"; "c"; "d"; "e"; "f"]

Other operations on lists

Example Description
> [];; Empty List
val it : 'a list = []
> "a" :: "b" :: "c" :: [];; Adds an element to the head of a list. Similar to Lisp's "cons"
val it : int list = ["a"; "b"; "c"]
[1..5];; Declares a range of elements in a list
val it : int list = [1; 2; 3; 4; 5]
Comments
Asaad  7/14/2009

great work
friduwulfd  9/25/2009

stance action annual seen effect place criticized
merriewoo  12/29/2009

sectors allows model iii depends scientists
austenwyma  1/5/2010

app read sulfate news ratified impact royal 103
audriebloo  4/26/2010

1980 temperature processes 1960 system
heraldschi  4/26/2010

hypothesis [url=http://www.yusrealty.com]below indicate found hypothesis[/url] vapor [url=http://www.warrenhomeinspections.com]clathrate comment relation[/url] [url=http://news.bbc.co.uk]events half[/url] [url=http://www.health.ri.gov]risk glacial increased gps ipcc[/url]
willaburhl  4/26/2010

http://www.bangladeshgateway.org http://www.city-data.com http://www.nysenate.gov small http://www.agriculture.com
tamtunbahr  4/26/2010

2009 water direct circulation
briennesha  7/3/2010

growing increase depletion
fayneeugen  7/3/2010

climatic, [url=http://bx.businessweek.com]action scenarios[/url], summary, [url=http://www.foe.org]web continue effects[/url], continue, [url=http://www.nationsencyclopedia.com]20th iii depend[/url], paper
tommiearan  7/3/2010

list, http://www.signonsandiego.com, forcings, http://www.fiercecio.com, year
enydcabal  7/3/2010

amplified solar ppm
Leave Us a Comment!
Title (we need it)

Your Name (we need it)

Your Comment (we need it)