F#在线运行

版本:
运行结果
教程手册
代码仓库
极速运行
交互输入
极速运行模式,更高效的运行,点击编辑器上方的运行按钮即刻体验吧。
以下是用户最新保存的代码
函数式 赛车模拟 (自由格式) 发布于:2021-02-02 10:52 转换OCaml对象模式 发布于:2021-02-01 00:46 [更多]
显示目录

模式匹配



模式匹配

模式匹配允许您“将数据与逻辑结构或结构进行比较,将数据分解为组成部分,或以各种方式从数据中提取信息”。
换句话说,它提供了一种更灵活和更强大的方法来根据一系列条件测试数据,并根据满足的条件执行一些计算。
从概念上讲,它就像一系列if ... then语句。

语法

在高级术语中,模式匹配遵循此语法F#

match expr with
| pat1 - result1
| pat2 -> result2
| pat3 when expr2 -> result3
| _ -> defaultResult

注意
每个| 符号定义了一个条件。
- >符号表示“如果条件为真,返回此值...”。
_符号提供默认模式,这意味着它匹配所有其他事情,如通配符。
实例
以下示例使用模式匹配语法计算斐波那契数字

let rec fib n =
   match n with
   | 0 -> 0
   | 1 -> 1
   | _ -> fib (n - 1) + fib (n - 2)
for i = 1 to 10 do
   printfn "Fibonacci %d: %d" i (fib i)

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

Fibonacci 1: 1
Fibonacci 2: 1
Fibonacci 3: 2
Fibonacci 4: 3
Fibonacci 5: 5
Fibonacci 6: 8
Fibonacci 7: 13
Fibonacci 8: 21
Fibonacci 9: 34
Fibonacci 10: 55

您也可以串联多个条件,其中返回相同的值在一起。例如

例2

let printSeason month =
   match month with
   | "December" | "January" | "February" -> printfn "Winter"
   | "March" | "April" -> printfn "Spring"
   | "May" | "June" -> printfn "Summer"
   | "July" | "August" -> printfn "Rainy"
   | "September" | "October" | "November" -> printfn "Autumn"
   | _ -> printfn "Season depends on month!"

printSeason "February"
printSeason "April"
printSeason "November"
printSeason "July"

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

Winter Spring Autumn Rainy

模式匹配函数

F#允许你写的模式匹配使用function关键字功能

let getRate = function
   | "potato" -> 10.00
   | "brinjal" -> 20.50
   | "cauliflower" -> 21.00
   | "cabbage" -> 8.75
   | "carrot" -> 15.00
   | _ -> nan (* nan is a special value meaning "not a number" *)

printfn "%g"(getRate "potato")
printfn "%g"(getRate "brinjal")
printfn "%g"(getRate "cauliflower")
printfn "%g"(getRate "cabbage")
printfn "%g"(getRate "carrot")

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

10
20.5
21
8.75
15

向模式添加过滤器或保护

您可以使用关键字when为模式添加过滤器或保护。
实例1

let sign = function
   | 0 -> 0
   | x when x < 0 -> -1
   | x when x > 0 -> 1

printfn "%d" (sign -20)
printfn "%d" (sign 20)
printfn "%d" (sign 0)

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

-1
1
0

例2

let compareInt x =
   match x with
   | (var1, var2) when var1 > var2 -> printfn "%d is greater than %d" var1 var2
   | (var1, var2) when var1 < var2 -> printfn "%d is less than %d" var1 var2
   | (var1, var2) -> printfn "%d equals %d" var1 var2

compareInt (11,25)
compareInt (72, 10)
compareInt (0, 0)

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

11 is less than 25
72 is greater than 10
0 equals 0

模式匹配元组

下面的示例演示元组匹配模式

let greeting (name, subject) =
   match (name, subject) with
   | ("Zara", _) -> "Hello, Zara"
   | (name, "English") -> "Hello, " + name + " from the department of English"
   | (name, _) when subject.StartsWith("Comp") -> "Hello,
   " + name + "from the department of Computer Sc."
   | (_, "Accounts and Finance")->
   "Welcome to the department of Accounts and Finance!"
   | _ -> "You are not registered into the system"

printfn "%s" (greeting ("Zara", "English"))
printfn "%s" (greeting ("Raman", "Computer Science"))
printfn "%s" (greeting ("Ravi", "Mathematics"))

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

Hello, Zara
Hello, Raman from the department of Computer Sc.
You are not registered into the system

模式匹配记录

下面的例子演示了记录模式匹配

type Point = { x: float; y: float }
let evaluatePoint (point: Point) =
   match point with
   | { x = 0.0; y = 0.0 } -> printfn "Point is at the origin."
   | { x = xVal; y = 0.0 } -> printfn "Point is on the x-axis. Value is %f." xVal
   | { x = 0.0; y = yVal } -> printfn "Point is on the y-axis. Value is %f." yVal
   | { x = xVal; y = yVal } -> printfn "Point is at (%f, %f)." xVal yVal

evaluatePoint { x = 0.0; y = 0.0 }
evaluatePoint { x = 10.0; y = 0.0 }
evaluatePoint { x = 0.0; y = 10.0 }
evaluatePoint { x = 10.0; y = 10.0 }

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

Point is at the origin.
Point is on the x-axis. Value is 10.000000.
Point is on the y-axis. Value is 10.000000.
Point is at (10.000000, 10.000000).
由JSRUN为你提供的F#在线运行、在线编译工具
        JSRUN提供的F#在线运行,F# 在线运行工具,基于linux操作系统环境提供线上编译和线上运行,具有运行快速,运行结果与常用开发、生产环境保持一致的特点。

title

使用此草稿 删除草稿

  • 00:23
注册登录后可减少验证码的弹出,点击前往 注册 | 登录

皮肤:

运行模式:

嵌入代码 iframe嵌入:


服务器已安装大部分常用的第依赖库,但仍可能存在一些未被安装的库, 可以通过本窗口立即安装所需依赖库。


请输入依赖库的名称:

请选择语言: