31. // ,
let p = " ", 21
type Person = {
Name: string
Age: int
}
let p = { Name = " "; Age = 21 }
4
32. F#
type Person(name: string, age: int) =
member this.Name = name
member this.Age = age
let p = Person(" ", 21)
type Person = { Name: string; Age: int }
let p = { Name = " "; Age = 21 }
33. type Person(name: string, age: int) =
member x.Name = name
member x.Age = age
let f p = p.Name
error FS0072:
34. type Person = { Name: string; Age: int }
let f p = p.Name
type Person =
{Name: string;
Age: int;}
val f : Person -> string
38. C#
public abstract class HtmlElem {}
public class Heading : HtmlElem {
public int Level { get; private set; }
public string Text { get; private set; }
public Heading(int level, string txt) {
Level = level; Text = txt; } }
public class Text : HtmlElem {
public string Text { get; private set; }
public Text(string txt) {
Text = txt; } }
public class HorizontalLine : HtmlElem {}
44. HtmlElem HTML
type HtmlElem =
| Heading of int * string
| Text of string
| HorizontalLine
let toHtmlStr elem =
let h lv s =
sprintf "<h%d>%s</h%d>" lv s lv
match elem with
| Heading(level, txt) -> h level txt
| Text txt -> "<p>" + txt + "</p>"
| HorizontalLine -> "<hr/>"
match
45. let add = function
| 0, y -> y
| x, 0 -> x
| x, y -> x + y
function
46. match function
match
let add tpl =
match tpl with
| x, y -> x + y
function
let add = function
| x, y -> x + y
47. type t = { Tag: int; Value: int }
let value { Value = v } = v
48. match
match
let add tpl =
match tpl with
| x, y -> x + y
function
let add = function
| x, y -> x + y
let add (x, y) = x + y
49. Person
type Name = {
FirstName: string
LastName: string
}
type Person = { Name: Name; Age: int }
let f x =
let { Name = { FirstName = fn } } = x
fn
51. Maybe
type MaybeBuilder() =
member this.Bind(x, f) =
x |> Option.bind f
member this.Return(x) = Some x
let maybe = MaybeBuilder()
let plus db = maybe {
let! x = db |> Map.tryFind "x"
let! y = db |> Map.tryFind "y"
return x + y
}
55. F#
fold reduce
> List.fold;;
val it : ((’a -> ’b -> ’a) -> ’a -> ’b list -> ’a)
= <fun:clo@1>
> List.reduce;;
val it : ((’a -> ’a -> ’a) -> ’a list -> ’a)
= <fun:clo@2-1>
fold
reduce list
list