本文共 3841 字,大约阅读时间需要 12 分钟。
let(|Dayu0|Xiaoyu0|Dengyu0|) value= if value<0 then Xiaoyu0 else if value=0 then Dengyu0 else Dayu0let test input = match input with | Dayu0 -> printfn ">0" | Xiaoyu0-> printfn "<0" | Dengyu0 -> printfn "=0"test 3test -2test 0
#lightopen System// 定义活动模式let (|Bool|Int|Float|String|) input = // attempt to parse a bool let success, res = Boolean.TryParse input if success then Bool(res) else // attempt to parse an int let success, res = Int32.TryParse input if success then Int(res) else // attempt to parse a float (Double) let success, res = Double.TryParse input if success then Float(res) else String(input)// 根据模式打印的函数// matching over the active patternlet printInputWithType input = match input with | Bool b -> printfn "Boolean: %b" b | Int i -> printfn "Integer: %i" i | Float f -> printfn "Floating point: %f" f | String s -> printfn "String: %s" s// print the resultsprintInputWithType "true"printInputWithType "12"printInputWithType "-12.1"
type option<'a> =| Some of 'a| None
#lightopen System.Text.RegularExpressions// 定义活动模式let (|Regex|_|) regexPattern input =// 创建,匹配正则表达式 let regex = new Regex(regexPattern) let regexMatch = regex.Match(input)// 返回 Some or None if regexMatch.Success then Some regexMatch.Value else None// function to print the results by pattern// matching over different instances of the// active patternlet printInputWithType input = match input with | Regex "$true|false^" s -> printfn "Boolean: %s" s | Regex @"$-?\d+^" s -> printfn "Integer: %s" s | Regex "$-?\d+\.\d*^" s -> printfn "Floating point: %s" s | _ -> printfn "String: %s" input// print the resultsprintInputWithType "true"printInputWithType "12"printInputWithType "-12.1"
[]type liter[ ]type pintlet vol1 = 2.5 let vol2 = 2.5 let newVol = vol1 + vol2
#light[]type liter[ ]type pintlet vol1 = 2.5 let vol2 = 2.5 // define the ratio of pints to literslet ratio = 1.0 / 1.76056338 // a function to convert pints to literslet convertPintToLiter pints = pints * ratio// perform the conversion and add the valueslet newVol = vol1 + (convertPintToLiter vol2)printfn "%A" newVol
转载地址:http://btrwx.baihongyu.com/