Your First F# program

#if you are the interviewee of SYSU-MSTC, congratulations, you passed the first interview and then choose the fascinating topic about F# for your second interview.

Introduction to F#

  • Functional Programming (FP) —— Most of interviewee mentioned that they have had knowledge about some sorts of program language like C/C++ or Python in the first round of interview. However, there are significant differences between F# and the languages mentioned above (maybe it can find some functional programming elements in Python syntax). It is common to classify C family language as procedural programming language, which contains a series of organized computational operations and execute by its order. By contrast, F#, the functional programming language, is much more similar to the concept of function in mathematical sense.  Functional programming languages treat data and operations as the some object, both of them can be interpret as a group of mapping relationships from some certain objects to corresponding objects. Admittedly, functional programming language is very friendly to beginner of computer science. Take MIT first year computer science course for example, they choose one of the principal dialects of Lisp (Scheme) to introduce the basic conceptions of computer science ans software engineering . Additionally, acquiring the knowledge of functional programming will benefit a lot to your understanding of C family language.
  • Modern Languages —— F# is one of the modern functional programming languages designed by Microsoft. If you read the previous interview topics, you will find that we have selected some kinds of functional programming languages such as Haskell as the interview topic. Haskell is also a very interesting language (but it will not appear in the interview again absolutely), and we do not decide to involve any point about Lisp and its dialect in interview for its beginner-unfriendly feature —— all of Lisp expression represented in Polish prefix notation (for instance, 3 + 2 – 9 will be written in prefix as – (+ 3 2) 9 ). Therefore, we finally choose F# the modern and fascinating functional Programming language as the topic.

1. Preparations

Visual Studio 2015 Community Download

Regretfully, Only Visual Studio 2010 and above version include F# complier due to Microsoft holds F# standard and develops it. Hence, if you work with only *nix environment, you may have to give up this fascinating and attractive topic and alter topic.

2. Design your first program


Click File – New Project – F# – Console Application in Visual Studio


Delete all demo codes


Type the following code:


let square x = x * x
let numbers = [1 .. 10]
let squares = square numbers
printfn "N^2 = %A" squares

open System

Press F5 to run


Next, Let‘s explain the meaning of this code

#light is a special Macros, we will talk about it later. What we have to know about it is that most of code need this Macros.

let square x = x * xF

This line of code defines a function called square. People with C programming experience may consider this line of code defines a function with name square and set variable x as x square. But it is misunderstood, one of the most essential conception in functional programming is that there is no traditional variable concept compared to procedural programming language. Therefore, assignment operation do not exist in functional programming (in F#, assignment can be implemented by special qualifier). The true meaning of the code is that it defines a function which accepts a parameters x, equivalent to declaring a variable. To sum up, this code define a function with argument x and it return the value x square.

let square = x * x (x)


let numbers = [1 .. 10]

It defines a list named numbers containing natural numbers from 1 to 10. It can find that there is no essential difference between data definition and function definition.

As for

let squares = square numbers

It Involves a slightly special concept. Here,  it can be interpret as a mapping relationships from list numbers to a list defined by where the mapping rule for each element in list is given by square.

The dependent variable set = rule, argument set)

And next we meet a familiar concept to C: printf function

printfn "N^2 = %A" squares

If you are familiar with C, you must know that “%A” is a placeholder representing output squares to the console in the position of “%A”.

The last two lines of code can be regarded as a command that waits for an input and then exit.

Now, you finish your first F# program.

However, this small program do not show all of the essence of functional programming languages. We just simply introduce some parts of F# syntax. For more detailed material about F#, you can refer to the relative chapters of MSDN or search for more information.

Wish everyone show themselves as well prepared in the upcoming second interview.

Leave a Reply

Your email address will not be published. Required fields are marked *