1.5.1 Use F # To write Hello World

Source: Internet
Author: User

1.5.1 Use F # To write Hello World

 

The easiest way to start using F # is to create a new script file. A script is a lightweight F # source file, which must belong to a project. The extension is usually. FSX. In Visual Studio, you can select File> New> file (or press Ctrl + n) and select F # script file from the script category. With this file, we can directly write the "Hello World" code.

 

Listing 1.10 printing Hello World (F #)

 

Let message = "Hello world! "[1]

Printfn "% s" Message [2]

 

Although this may not be the simplest "Hello World" in F #, it is quite difficult to write anything meaningful with only one line of code. Listing 1.10 starts with a value bound to [1], which is similar to a variable declaration, but there is an important difference: the value cannot be changed in the future. This fully complies with the functional style, making everything immutable (the next two chapters will learn about the content ).

After the Hello world value is assigned to the symbolic message, the program then calls the printfn function [2]. It is worth noting that F # function parameters are generally separated by spaces without parentheses or commas. Occasionally, parentheses are used to make the code more readable. For example, cos (1.57). Even in this case, parentheses are optional. In the following chapters, we will explain the conventions used as we learn the core concepts of F.

The first parameter of the printfn function is a format string. In this example, it specifies that the function should have only one additional parameter and be a string. The parameter type is specified by % s in the format string, and the parameter type is checked by the compiler. Now we will explain how the code runs (listing 1.11 ).

 

Prompt

 

The simplest way to run code is to use an interactive tool in the F # toolset. With these toolsets, you can develop interactive styles. In this way, you can easily see what the code is doing and verify that the input sample runs correctly. Some languages have an interactive console that can paste and execute code. This is called read-eval-print, REPL, because the code is calculated immediately.

In F #, F # interactive can be used at a command prompt and integrated into the Visual Studio environment. In this way, writing code not only provides complete IDE and SMART awareness support, but also supports selecting a piece of code and executing it immediately for testing.

 

If you use F # interactive in the command line, paste the code above, type two semicolons (;), and press the Enter key to execute.

If Visual Studio is used, select the code, and press Alt + enter, the code will be sent to the interactive window, [and run]. Let's take a look at the results of running this code.

 

Listing 1.11 running the hello worldprogram (F # interactive)

 

Microsoft F # interactive, (c) microsoftcorporation, All Rights Reserved

F # version 1.9.7.4, compiling for. netframework version v2.0.50727

> (...);;

Hello world! [1]

Val message: String = "helloworld! "[2]

 

The first line [1] is the output of the printfn function. It prints the string and does not return any value. The second line [2] is generated by the value binding. The report declares the value message, string type. We didn't specify the type, but F # used a technology called type inference to export the value type, so the program is a static type, just like a program in C.

Writing examples similar to "Hello World" does not demonstrate the performance of F # in larger scale. The general F # development process is worth looking at, because it makes more sense.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.