codecamp

F#记录

记录类似于一个元组,但它包含命名字段。 例如,

type website =
   { title : string;
      url : string }

定义记录

一个记录被定义为使用type关键字类型,记录的字段被定义为一个分号分隔的列表。

语法定义一个记录 

type recordName =
   { [ fieldName : dataType ] + }

创建记录

您可以通过指定记录的字段创建一个记录。例如,让我们创建一个网站记录中名为homepage

let homepage = { Title = "TutorialsPoint"; Url = "www.w3cschool.cn" }

下面的实例将解释其中的概念 

例1

此程序定义一个名为website的记录类型。 然后它创建一些类型的website记录并打印记录。

(* defining a record type named website *)
type website =
   { Title : string;
      Url : string }

(* creating some records *)
let homepage = { Title = "TutorialsPoint"; Url = "www.w3cschool.cn" }
let cpage = { Title = "Learn C"; Url = "www.w3cschool.cn/c/index.htm" }
let fsharppage = { Title = "Learn F#"; Url = "www.w3cschool.cn/fsharp/index.htm" }
let csharppage = { Title = "Learn C#"; Url = "www.w3cschool.cn/csharp/index.htm" }

(*printing records *)
(printfn "Home Page: Title: %A 
 	 URL: %A") homepage.Title homepage.Url
(printfn "C Page: Title: %A 
 	 URL: %A") cpage.Title cpage.Url
(printfn "F# Page: Title: %A 
 	 URL: %A") fsharppage.Title fsharppage.Url
(printfn "C# Page: Title: %A 
 	 URL: %A") csharppage.Title csharppage.Url

当你编译和执行程序,它产生以下输出 

Home Page: Title: "TutorialsPoint"
       URL: "www.w3cschool.cn"
C Page: Title: "Learn C"
      URL: "www.w3cschool.cn/c/index.htm"
F# Page: Title: "Learn F#"
      URL: "www.w3cschool.cn/fsharp/index.htm"
C# Page: Title: "Learn C#"
      URL: "www.w3cschool.cn/csharp/index.htm"

例2

type student =
   { Name : string;
      ID : int;
      RegistrationText : string;
      IsRegistered : bool }

let getStudent name id =
   { Name = name; ID = id; RegistrationText = null; IsRegistered = false }

let registerStudent st =
   { st with
      RegistrationText = "Registered";
      IsRegistered = true }

let printStudent msg st =
   printfn "%s: %A" msg st

let main() =
   let preRegisteredStudent = getStudent "Zara" 10
   let postRegisteredStudent = registerStudent preRegisteredStudent

   printStudent "Before Registration: " preRegisteredStudent
   printStudent "After Registration: " postRegisteredStudent

main()

当你编译和执行程序,它产生以下输出 

Before Registration: : {Name = "Zara";
   ID = 10;
   RegistrationText = null;
   IsRegistered = false;}
After Registration: : {Name = "Zara";
   ID = 10;
   RegistrationText = "Registered";
   IsRegistered = true;}
F#元组
F#列表
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }