There are two different ways to create record. The first way:> type FamilyMember = {Name: string; relation: string};; type FamilyMember = {Name: string; relation: string;}
Or a more explicit syntax:> {Name = "Nate";relation = "Father"};; val it : FamilyMember = {Name = "Nate"; relation = "Father";}
F# makes it easy for you to access the record labels:> {new FamilyMember with Name = "Kelly" and relation = "Mother"};; val it : FamilyMember = {Name = "Kelly"; relation = "Mother";}
Records can be cloned, and like everything else with F#, it’s amazingly simple!> let f = {new FamilyMember with Name = "Nate" and relation = "father"};; val f : FamilyMember > f.Name;; val it : string = "Nate"
Records can be results of functions as well. Take the following type, which has a count of files and a count of directories:> let d1 = {new FamilyMember with Name = "reilly" and relation = "daughter"};; val d1 : FamilyMember > let d2 = {d1 with Name = "Cora"};; val d2 : FamilyMember > d2;; val it : FamilyMember = {Name = "Cora"; relation = "daughter";}
We can use it for a function like so:type FilesAndDirectorys = {Files: int; Directories: int;}
Next call the function with the path you want to searchlet GetFilesAndDirectories loc = let dirs = Directory.GetDirectories(loc) let files = Directory.GetFiles(loc) let results = {new FilesAndDirectorys with Files = files.Length and Directories = dirs.Length} results
let counts = GetFilesAndDirectories @"C:\" The identifier counts now contains the following data:There is another type, discriminated union types, but I'll save those for another blog entry.val it : FilesAndDirectorys = {Files = 9; Directories = 18;}
No comments:
Post a Comment