5.5.1.1 类型批注、动作和语句块(TYPE ANNOTATIONS, ACTIONS, AND STATEMENT BLOCKS)

5.5.1.1 类型批注、动作和语句块(TYPE ANNOTATIONS, ACTIONS, AND STATEMENT BLOCKS)

 

在前面的示例中,我们没有显式指定参数类型。这在 F# 中是正常行为,因为,类型推断能力非常强大,而且,在前面的示例中,有足够的线索能推断出类型。在 C# 中,是另一种方式,也相当重要:

 

Func toStr1 = num => num.ToString();

Func toStr2 = (int num) =>num.ToString();

 

两行代码基本相同,唯一区别在于,第二行显式指定 num 参数的类型。两行都是正确的,那么,C# 如何知第一行中 num 的类型呢?答案是,类型来自变量的声明。它知道,Func < int, string> 是一个委托,取一个整数作为参数值,因此,推断 num 的类型应该为整数。

在 C# 中,很少需要显式参数类型;C# 通常能够推断出类型,但不管怎样,都不能使用 var 关键字声明 lambda 函数。有一个例外情况,使用 lambda 函数作为特定泛型方法的参数值。即使在 F# 中,我们偶尔也可能需要给编译器以更多的信息,即,使用类型批注(type annotations)。清单 5.16 显示了带有类型批注的 lambda 函数,显式声明参数的类型。

 

清单 5.16 高级 lambda 函数 (F# Interactive, C#)

// F# version of the code (using F#Interactive)

> let sayHello =

   (fun(str:string) –>  [1]  类型批注

     letmsg = str.Insert(0, "Hello ")

     Console.WriteLine(msg)

   )

val sayHello : string –> unit

 

// C# version of the code

Action sayHello =  [2]  动作委托

  str=> {    [3]  语句块语法

    varmsg = str.Insert(0, "Hello ");

    Console.WriteLine(msg);

};

 

这个示例展示了几件重要的事情。第一件是在 F# 中使用类型批注[1]。在 lambda 函数中的类型批注,其语法与 F# 代码中的其他任何地方都一样。在这里,我们之所以要使用它的原因是,调用值 str 的实例方法 Insert,它不能给编译器提供足够的信息来确定值的类型。

另一件值得注意的事情是,lambda 函数体不仅仅是表达式。在 F# 中,我们添加了一个 let 绑定,把整个 lambda 函数括在括号中。在 C# 版本中,我们添加了一个变量声明,改变语法为使用语句块(statement block)。语句块,是括在大括号内的lambda 函数体[3],能够在函数体内有多条语句。要从使用语句块的lambda 函数返回结果,必须使用 return 关键字,就如同从方法返回结果一样。

在这个示例中,lambda 函数不返回结果。在 F# 中,unit 是一个普通类型,推导出的函数签名是 string -> unit。原则上,这就是一个普通的 F# 函数,结果返回 unit 值(即,什么也没有);但在 C# 中,不能写成 Func<string, void>,因为,void 不是真正的类型。为此,C#有另外一族委托类型,称为Action(动作)[2],表示没有返回类型的 lambda 函数。Action 和 Func 委托非常有用,对应于 F# 中的函数类型,因此,我们需要详细了解函数值的类型。

【四轴飞行器】非线性三自由度四轴飞行器模拟器研究(Matlab代码实现)内容概要:本文围绕非线性三自由度四轴飞行器模拟器的研究展开,重点介绍了基于Matlab的建模与仿真方法。通过对四轴飞行器的动力学特性进行分析,构建了非线性状态空间模型,并实现了姿态与位置的动态模拟。研究涵盖了飞行器运动方程的建立、控制系统设计及数值仿真验证等环节,突出非线性系统的精确建模与仿真优势,有助于深入理解飞行器在复杂工况下的行为特征。此外,文中还提到了多种配套技术如PID控制、状态估计与路径规划等,展示了Matlab在航空航天仿真中的综合应用能力。; 适合人群:具备一定自动控制理论基础Matlab编程能力的高校学生、科研人员及从事无人机系统开发的工程技术人员,尤其适合研究生及以上层次的研究者。; 使用场景及目标:①用于四轴飞行器控制系统的设计与验证,支持算法快速原型开发;②作为教学工具帮助理解非线性动力学系统建模与仿真过程;③支撑科研项目中对飞行器姿态控制、轨迹跟踪等问题的深入研究; 阅读建议:建议读者结合文中提供的Matlab代码进行实践操作,重点关注动力学建模与控制模块的实现细节,同时可延伸学习文档中提及的PID控制、状态估计等相关技术内容,以全面提升系统仿真与分析能力。
Arction LightningChart for .NET v12.0.1.1 is a high - performance charting library designed for .NET developers. ### Function Introduction - **High - Performance Rendering**: It can handle large - scale data sets efficiently, providing smooth and real - time data visualization. For example, in financial applications, it can display high - frequency trading data without lag. - **Multiple Chart Types**: Supports a wide variety of chart types such as line charts, bar charts, scatter plots, and 3D charts. This allows developers to choose the most appropriate chart type according to different data characteristics and application scenarios. - **Customization**: Developers can customize the appearance of charts, including colors, fonts, and line styles. They can also add custom labels, annotations, and legends to enhance the readability of the charts. - **Interactive Features**: Offers interactive features like zooming, panning, and tooltips. These features enable users to explore data in more detail and gain deeper insights. ### Download The official website of Arction is the primary source for downloading LightningChart for .NET v12.0.1.1. Usually, developers need to register an account on the official website. After registration, they can find the corresponding version in the download section. Sometimes, a trial version is also available for users to evaluate the software before purchasing. ### Usage Guide 1. **Installation**: After downloading the installation package, run the installer and follow the prompts to complete the installation on the local development environment. 2. **Project Integration**: In a .NET project (such as a Visual Studio project), add a reference to the LightningChart library. This can be done by right - clicking on the project in the solution explorer, selecting "Add Reference", and then browsing to the installed library files. 3. **Code Implementation**: ```csharp using Arction.Wpf.Charting; using System.Windows; namespace LightningChartExample { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); // Create a new LightningChart object LightningChartUltimate chart = new LightningChartUltimate(); // Add the chart to the window this.Content = chart; // Create a new line series SeriesXY lineSeries = chart.ViewXY.Series.AddSeriesXY(); // Add data points to the line series lineSeries.Points.Add(new System.Windows.Point(1, 2)); lineSeries.Points.Add(new System.Windows.Point(2, 4)); lineSeries.Points.Add(new System.Windows.Point(3, 6)); // Refresh the chart to display the data chart.Invalidate(); } } } ``` 4. **Testing and Debugging**: Run the project to test whether the chart is displayed correctly. If there are any issues, use the debugging tools in the development environment to find and fix the problems.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值