[转]Walkthrough: Your First F# Program
本文转自:http://msdn.microsoft.com/en-us/library/vstudio/dd233160(v=vs.100).aspx
Visual Studio 2010 includes a new programming language, F#. F# is a multiparadigm language that supports functional programming in addition to traditional object-oriented programming and .NET concepts. The following examples introduce some of its features and syntax. The examples show how to declare simple variables, to write and test functions, to create tuples and lists, and to define and use a class.
![]() |
---|
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings. |
To create a new console application
On the File menu, point to New, and then click Project.
If you cannot see Visual F# in the Templates Categories pane, click Other Languages, and then click Visual F#. The Templates pane in the center lists the F# templates.
Look at the top of the Templates pane to make sure that .NET Framework 4 appears in the Target Framework box.
Click F# Application in the list of templates.
Type a name for your project in the Name field.
Click OK.
The new project appears in Solution Explorer.
To use the let keyword to declare and use identifiers
Copy and paste the following code into Program.fs. You are binding each identifier, anInt, aString, and anIntSquared, to a value.
Note
If you cannot see the code in Classic view, make sure that the Language Filter in the header below the topic title is set to include F#.
To see results in the F# Interactive window
Select the let expressions in the previous procedure.
Right-click the selected area and then click Send to Interactive. Alternatively, press ALT+ENTER.
The F# Interactive window opens and the results of interpreting the let expressions are displayed, as shown in the following lines. The types are inferred from the specified values.
val anInt : int = 5
val aString : string = "Hello"
val anIntSquared : int = 25
To see the results in a Command Prompt window
Add the following lines to Program.fs.
Press CTRL+F5 to run the code. A Command Prompt window appears that contains the following values.
5
Hello
25
You can verify the inferred types by resting the mouse pointer on the identifier names anInt, aString, and anIntSquared in the previous WriteLine statements.
To define and run a function
Use a let expression to define a squaring function, as shown in the following code. The function has one parameter, n, and returns the square of the argument sent to n.
Press CTRL+F5 to run the code. The result displayed is 25.
A recursive function requires a let rec expression. The following example defines a function that calculates the factorial of parameter n.
Press CTRL+F5 to run the function. The result displayed is 120, the factorial of 5.
To create collections: lists and tuples
One way to aggregate values is by using a tuple, as shown in the following code.
Another way to aggregate values is by using a list, as shown in the following code.
Add a new best friend to the list by using the "cons" operator (::). Note that the operation does not change the value of bffs. The value of bffs is immutable and cannot be changed.
Use printfn to display the lists. Function printfn shows the individual elements that are contained in structured values.
You can view the results either by pressing CTRL+F5 or by selecting a section of the code and then pressing ALT+ENTER.
To create and use a class
The following code creates a Person class that has two properties, Name and Age. Name is a read-only property. Its value is immutable, as are most values in functional programming. You can create mutable values in F# if you need them, but you must explicitly define them as mutable. In the following class definition, the value of Age is stored in a mutable local variable, internalAge. The value of internalAge can be changed.
// The declaration creates a constructor that takes two values, name and age.
type Person(name:string, age:int) =
// A Person object's age can be changed. The mutable keyword in the
// declaration makes that possible.
let mutable internalAge = age // Declare a second constructor that takes only one argument, a name.
// This constructor calls the constructor that requires two arguments,
// sending 0 as the value for age.
new(name:string) = Person(name, 0) // A read-only property.
member this.Name = name
// A read/write property.
member this.Age
with get() = internalAge
and set(value) = internalAge <- value // Instance methods.
// Increment the person's age.
member this.HasABirthday () = internalAge <- internalAge + 1 // Check current age against some threshold.
member this.IsOfAge targetAge = internalAge >= targetAge // Display the person's name and age.
override this.ToString () =
"Name: " + name + "\n" + "Age: " + (string)internalAgeTo test the class, declare two Person objects, make some changes, and display the results, as shown in the following code.
// The following let expressions are not part of the Person class. Make sure
// they begin at the left margin.
let person1 = Person("John", 43)
let person2 = Person("Mary") // Send a new value for Mary's mutable property, Age.
person2.Age <- 15
// Add a year to John's age.
person1.HasABirthday() // Display results.
System.Console.WriteLine(person1.ToString())
System.Console.WriteLine(person2.ToString())
// Is Mary old enough to vote?
System.Console.WriteLine(person2.IsOfAge(18))The following lines are displayed.
Name: John
Age: 44
Name: Mary
Age: 15
False
To view other examples in the F# tutorial
On the File menu, point to New, and then click Project.
If you cannot see Visual F# in the Templates Categories pane, click Other Languages, and then click Visual F#. The Templates pane in the center lists the F# templates.
Look at the top of the Templates pane to make sure that .NET Framework 4 appears in the Target Framework box.
Click F# Tutorial in the list of templates.
Click OK.
The tutorial appears in Solution Explorer.
For more information about functional programming and additional examples, see Functions as First-Class Values (F#). For more information about tuples, lists, let expressions, function definitions, classes, members, and many other topics, see F# Language Reference.
Concepts
Other Resources
[转]Walkthrough: Your First F# Program的更多相关文章
- awk -f program.file 功能使用
一.awk -f program.file 功能使用 一直没有使用过awk的-f功能,感觉鸡肋,不是很实用,更多的是因为没有需求的原因 下面介绍下awk -f的使用方法 awk可以指定默认的文件路径, ...
- [转]Visual F# Samples and Walkthroughs
本文转自:http://msdn.microsoft.com/en-US/library/vstudio/ee241126.aspx This topic provides links to samp ...
- 如果你也会C#,那不妨了解下F#(1):F# 数据类型
本文链接:http://www.cnblogs.com/hjklin/p/fs-for-cs-dev-1.html 简单介绍 F#(与C#一样,念作"F Sharp")是一种基于. ...
- F#(1)
如果你也会C#,那不妨了解下F#(1):F# 数据类型 简单介绍 F#(与C#一样,念作“F Sharp”)是一种基于.Net框架的强类型.静态类型的函数式编程语言.可以说C#是一门包含函数式编程 ...
- GLSL着色语言学习。橙皮书第一个例子GLSL+OpenTK+F#的实现。
Opengl红皮书有选择的看了一些,最后的讲着色语言GLSL的部分看的甚为不理解,然后找到Opengl橙皮书,然后就容易理解多了. 在前面,我们或多或少接触到Opengl的处理过程,只说前面一些处理, ...
- [译]Python中的异步IO:一个完整的演练
原文:Async IO in Python: A Complete Walkthrough 原文作者: Brad Solomon 原文发布时间:2019年1月16日 翻译:Tacey Wong 翻译时 ...
- Tesseract-OCR字符识别简介
OCR(Optical Character Recognition):光学字符识别,是指对图片文件中的文字进行分析识别,获取的过程.Tesseract:开源的OCR识别引擎,初期Tesseract引擎 ...
- 开源发布:VS代码段快捷方式及可视化调试快速部署工具
前言: 很久前,我发过两篇文章,分别介绍自定义代码版和可视化调试: 1:Visual Studio 小技巧:自定义代码片断 2:自定义可视化调试工具(Microsoft.VisualStudio.De ...
- 搭建selenium grid简单配置
1.使用selenium提供的服务端独立jar包 :服务端.客户端都是运行于java7环境. 2.启动hub: hub配置文件如下: Java -jar selenium-server-standal ...
随机推荐
- Zookeeper开发常见问题
背景与目的 Zookeeper开发过程中遇到一些常见问题,为了后续开发不犯同样的错误,总结一下此类问题,并进行分析和解决. 适合人员 主要适合zookeeper开发.测试及运维相关人员. 问题与解决 ...
- 【Mongodb教程 第一课 补加课】 Failed to connect to 127.0.0.1:27017, reason: errno:10061 由于目标计算机积极拒绝,无法连接
1:启动MongoDB 2014-07-25T11:00:48.634+0800 warning: Failed to connect to 127.0.0.1:27017, reason: errn ...
- struts2的文件上传机制
Struts2的上传(基本流程例如以下) 1.Struts2默认採用了apache commons-fileupload 2.Struts2支持三种类型的上传组件 3.须要引入commons-file ...
- Linux Shell_test
test: 测试Shell脚本里的条件,通过推出状态返回其结果.用法: test [ expression ] 或 [ [ expression ] ] 注意空格test表达式:是则为真 ...
- 2016/5/6 thinkphp ①框架 ② 框架项目部署 ③MVC模式 ④控制器访问及路由解析 ⑤开发和生产模式 ⑥控制器和对应方法创建 ⑦视图模板文件创建 ⑧url地址大小写设置 ⑨空操作空控制器 ⑩项目分组
真实项目开发步骤: 多人同时开发项目,协作开发项目.分工合理.效率有提高(代码风格不一样.分工不好) 测试阶段 上线运行 对项目进行维护.修改.升级(单个人维护项目,十分困难,代码风格不一样) 项目稳 ...
- git unstage
https://stackoverflow.com/questions/6919121/why-are-there-2-ways-to-unstage-a-file-in-git git rm --c ...
- YTU 2953: A代码填充--学画画
2953: A代码填充--学画画 时间限制: 1 Sec 内存限制: 128 MB 提交: 62 解决: 52 题目描述 最近小平迷上了画画,经过琨姐的指导,他学会了RGB色彩的混合方法.对于两种 ...
- jQuery简单纯文字提示条
如何制作jQuery简单纯文字提示条,先介绍提示条(tooltip)的意思是用户鼠标悬停经过事件发生提示title.它们已经呈现在大多数浏览器中,当你可以提供一个链接或图片的title属性,就是用户将 ...
- 什么叫强类型的DATASET ?对DATASET的操作处理?强类型DataSet的使用简明教程
强类型DataSet,是指需要预先定义对应表的各个字段的属性和取值方式的数据集.对于所有这些属性都需要从DataSet, DataTable, DataRow继承,生成相应的用户自定义类.强类型的一个 ...
- RMQ(Range Minimum Query)问题(转)
问题描述 RMQ问题是求给定区间中的最值问题.对于长度为n的数列A,回答若干查询RMQ(A, i, j).返回数组A中下标在[i,j]里的最小值的下标. 比如数列 5,8,1,3,6,4,9,5,7 ...