F # Learning Road (6) List type

Source: Internet
Author: User
Tags expression

Lists occupy an important place in functional programming. In the Lisp language, everything is a list, and the function is also a list, and the list is the ultimate in Lisp. The F # language list syntax is derived from OCaml and is basically consistent with the Haskell language. This article will only explain some commonly used methods, to a good grasp of the list, friends can google the relevant content. Most of the discussion on the list on the network is Haskell language (the Lisp list is powerful, but it's too different from most functional language lists).

One, how to define the list

There are several ways to define a list in F #.

1. Use character value

let l=[1;2;3]
let emptyList=List.empty
let lista=l @ emptyList
let listb=0::lista

Use the brackets ' [', '] ', separating the elements with semicolons. Unlike tuple types, tuples use ' (', ') ', and elements are separated by commas.

The lists in F # are linked lists, the list elements must be the same type, and the list is immutable, so the definition will not change.

There are three ways to define an empty list in F #. There are some subtle differences in their use.

printfn "%A" ((List.empty<int>).GetType())
printfn "%A" ((List.Empty:int list).GetType())
printfn "%A" (([]:int list).GetType())

A @ operator is defined on the list to append a list to the tail of another list and generate a new list. And:: operator, then add an element to the header of a list, forming a new list. Note that the list of all actions does not change itself. This behavior is similar to the. NET string type.

2, the use of range expression

let l1=[1..10]
let l2=[1..2..100]
let l3=['a'..'z']

3, the use of sequence expressions

let l4=[for i in 1..10 do if i>4 && i<8 then yield i]

As for the sequence to be discussed in a future blog, a sequence is an implementation of a computational expression.

Second, list comparison.

printfn "%b" ([1;2;3]>[1;2]) //true
printfn "%b" ([1;2]=[1;4]) //false

List types make it easy to compare values, use obj for reference comparisons. ReferenceEquals

printfn "%b" (obj.ReferenceEquals([],[])) //true
printfn "%b" (obj.ReferenceEquals([1],[1])) //false

Third, access list elements

printfn "%.2f" ([1.;2.].[1]) //2.00

The list index starts at 0.

Iv. using list Type properties

printfn "%d" ([1..10].Length) //10
printfn "%d" ([1;2;3].Head) //1
printfn "%A" ([1;2;3].Tail) //[2;3]
printfn "%A" ([1;2;3].IsEmpty) //false

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.