官术网_书友最值得收藏!

Talk is cheap, show me some code

In the next screenshot, you will see a simple print statement (Hello World). This statement is executed in the interactive environment by highlighting the statement (you do not need to highlight [<EntryPoint>]) and then pressing Alt + Enter. You will see the output of the command in the FSI window as shown in the following screenshot:

You can also run the program from the context menu by selecting the code to execute and right-clicking in the code window to launch the context menu, followed by selecting the Execute in Interactive option. The following screenshot shows the context menu:

F# interactive provides a large set of features that we will use throughout this book. If you are interested in knowing more about FSI, please refer to the FSI Reference on MSDN at http://msdn.microsoft.com/en-us/library/dd233175.aspx.

Now that we have run our first program, let's do some Math. is The F# let statement is used to bind an identifier, which can be a value or a function. In the following screenshot, we define a multiply function that will take two variables as arguments, and return their product.

You will now start to realize the terseness of the F# syntax; the missing parenthesis, lack of type declarations, and no return statement! Upon executing the line of code by selecting it and pressing Alt + Enter (or selecting and running it from the context menu as shown in the preceding screenshot), we can now execute the function.

We typed Multiply 10 10 in the FSI window, followed by ;; (a terminating token, required only in interactive mode), and saw the result (along with the resulting type) as 100. This is the power of REPL or the Console Prompt that allows you to run commands in an interactive manner.

Because we don't need much code ceremony and boilerplate bloat, it is simple and efficient to build and execute algorithms in F#.

Before we proceed further, let's quickly examine F# project types in Visual Studio which are as follows:

  • Console Application: This creates command line applications
  • F# Library: This creates an F# library; it can be used from other programming languages
  • Tutorial: This is a tutorial walk-through; it is highly recommended for beginners
  • Portable Library (legacy and new): This is a portable library for .NET 4.5 and Windows Store
  • Silverlight Library: This is an F# Silverlight library to be used in Silverlight applications

Since the focus of this text is mainly on data structures and algorithms, we will concentrate on console applications and F# libraries. Also for F# File types, the scripts use the file extension.fsx or.fsscript while the source code files use the.fs extension. F# has three different types of environments namely interactive, scripting, and compiled environments. We will see more, as we work through examples.

主站蜘蛛池模板: 庄河市| 牙克石市| 阜南县| 广灵县| 衡阳市| 银川市| 苏尼特右旗| 南丰县| 黔西县| 二连浩特市| 三门峡市| 凭祥市| 遂宁市| 汶川县| 金塔县| 伊宁县| 兰州市| 甘德县| 开阳县| 三亚市| 英山县| 增城市| 永宁县| 湟中县| 马公市| 新平| 类乌齐县| 宁强县| 石渠县| 仙桃市| 宁国市| 湾仔区| 涪陵区| 杨浦区| 杂多县| 鹤壁市| 张北县| 铜陵市| 蒲城县| 赤壁市| 平陆县|