পৃষ্ঠাসমূহ

.

Search Your Article

Total Pageviews

Wednesday, February 1, 2017

F# - Quick Guide

F# - Overview

F# is a functional programming language. To understand F# constructs, you need to read a couple of lines about the programming paradigm named Functional Programming.
Functional programming treats computer programs as mathematical functions. In functional programming, the focus would be on constants and functions, instead of variables and states.
Because functions and constants are things that don’t change.
In functional programming, you will write modular programs, i.e., the programs would consist of functions that will take other functions as input.
Programs written in functional programming language tend to be concise.

About F#

Following are the basic information about F# −
  • It was developed in 2005 at Microsoft Research.
  • It is a part of Microsoft’s family of .Net language.
  • It is a functional programming language.
  • It is based on the functional programming language OCaml.

Features of F#

  • It is .Net implementation of OCaml.
  • It compiles .Net CLI (Common Language Interface) byte code or MSIL (Microsoft Intermediate Language) that runs on CLR (Common Language Runtime).
  • It provides type inference.
  • It provides rich pattern matching constructs.
  • It has interactive scripting and debugging capabilities.
  • It allows writing higher order functions.
  • It provides well developed object model.

Use of F#

F# is normally used in the following areas −
  • Making scientific model
  • Mathematical problem solving
  • Artificial intelligence research work
  • Financial modelling
  • Graphic design
  • CPU design
  • Compiler programming
  • Telecommunications
It is also used in CRUD apps, web pages, GUI games and other general purpose programs.

F# - Environment Setup

The tools required for F# programming are discussed in this chapter.

Integrated Development Environment(IDE) for F#

Microsoft provides Visual Studio 2013 for F# programming.
The free Visual Studio 2013 Community Edition is available from Microsoft’s official website. Visual Studio 2013 Community and above comes with the Visual F# Tools. The Visual F# Tools include the command-line compiler (fsc.exe) and F# Interactive (fsi.exe).
Using these tools, you can write all kinds of F# programs from simple command-line applications to more complex applications. You can also write F# source code files using a basic text editor, like Notepad, and compile the code into assemblies using the command-line compiler.
You can download it from Microsoft Visual Studio. It gets automatically installed in your machine.

Writing F# Programs On Links

Please visit the F# official website for the latest instructions on getting the tools as a Debian package or compiling them directly from the source − http://fsharp.org/use/linux/.

Try it Option Online

We have set up the F# Programming environment online. You can easily compile and execute all the available examples online along with doing your theory work. It gives you confidence in what you are reading and to check the result with different options. Feel free to modify any example and execute it online.
Try the following example using the Try it option or use the url − http://www.compileonline.com/.
(* This is a comment *)
(* Sample Hello World program using F# *)
printfn "Hello World!"
For most of the examples given in this tutorial, you will find a Try it option in our website code sections at the top right corner that will take you to the online compiler. So just make use of it and enjoy your learning.

F# - Program Structure

F# is a Functional Programming language.
In F#, functions work like data types. You can declare and use a function in the same way like any other variable.
In general, an F# application does not have any specific entry point. The compiler executes all top-level statements in the file from top to bottom.
However, to follow procedural programming style, many applications keep a single top level statement that calls the main loop.
The following code shows a simple F# program −
open System
(* This is a multi-line comment *)
// This is a single-line comment

let sign num =
   if num > 0 then "positive"
   elif num < 0 then "negative"
   else "zero"

let main() =
   Console.WriteLine("sign 5: {0}", (sign 5))

main()
When you compile and execute the program, it yields the following output −
sign 5: positive
Please note that −
  • An F# code file might begin with a number of open statements that is used to import namespaces.
  • The body of the files includes other functions that implement the business logic of the application.
  • The main loop contains the top executable statements.

F# - Basic Syntax

You have seen the basic structure of an F# program, so it will be easy to understand other basic building blocks of the F# programming language.

Tokens in F#

An F# program consists of various tokens. A token could be a keyword, an identifier, a constant, a string literal, or a symbol. We can categorize F# tokens into two types −
  • Keywords
  • Symbol and Operators

F# Keywords

The following table shows the keywords and brief descriptions of the keywords. We will discuss the use of these keywords in subsequent chapters.
Keyword Description
abstract Indicates a method that either has no implementation in the type in which it is declared or that is virtual and has a default implementation.
and Used in mutually recursive bindings, in property declarations, and with multiple constraints on generic parameters.
as Used to give the current class object an object name. Also used to give a name to a whole pattern within a pattern match.
assert Used to verify code during debugging.
base Used as the name of the base class object.
begin In verbose syntax, indicates the start of a code block.
class In verbose syntax, indicates the start of a class definition.
default Indicates an implementation of an abstract method; used together with an abstract method declaration to create a virtual method.
delegate Used to declare a delegate.
do Used in looping constructs or to execute imperative code.
done In verbose syntax, indicates the end of a block of code in a looping expression.
downcast Used to convert to a type that is lower in the inheritance chain.
downto In a for expression, used when counting in reverse.
elif Used in conditional branching. A short form of else if.
else Used in conditional branching.
end In type definitions and type extensions, indicates the end of a section of member definitions.
In verbose syntax, used to specify the end of a code block that starts with the begin keyword.
exception Used to declare an exception type.
extern Indicates that a declared program element is defined in another binary or assembly.
false Used as a Boolean literal.
finally Used together with try to introduce a block of code that executes regardless of whether an exception occurs.
for Used in looping constructs.
fun Used in lambda expressions, also known as anonymous functions.
function Used as a shorter alternative to the fun keyword and a match expression in a lambda expression that has pattern matching on a single argument.
global Used to reference the top-level .NET namespace.
if Used in conditional branching constructs.
in Used for sequence expressions and, in verbose syntax, to separate expressions from bindings.
inherit Used to specify a base class or base interface.
inline Used to indicate a function that should be integrated directly into the caller's code.
interface Used to declare and implement interfaces.
internal Used to specify that a member is visible inside an assembly but not outside it.
lazy Used to specify a computation that is to be performed only when a result is needed.
let Used to associate, or bind, a name to a value or function.
let! Used in asynchronous workflows to bind a name to the result of an asynchronous computation, or, in other computation expressions, used to bind a name to a result, which is of the computation type.
match Used to branch by comparing a value to a pattern.
member Used to declare a property or method in an object type.
module Used to associate a name with a group of related types, values, and functions, to logically separate it from other code.
mutable Used to declare a variable, that is, a value that can be changed.
namespace Used to associate a name with a group of related types and modules, to logically separate it from other code.
new Used to declare, define, or invoke a constructor that creates or that can create an object.
Also used in generic parameter constraints to indicate that a type must have a certain constructor.
not Not actually a keyword. However, not struct in combination is used as a generic parameter constraint.
null Indicates the absence of an object.
Also used in generic parameter constraints.
of Used in discriminated unions to indicate the type of categories of values, and in delegate and exception declarations.
open Used to make the contents of a namespace or module available without qualification.
or Used with Boolean conditions as a Boolean or operator. Equivalent to ||.
Also used in member constraints.
override Used to implement a version of an abstract or virtual method that differs from the base version.
private Restricts access to a member to code in the same type or module.
public Allows access to a member from outside the type.
rec Used to indicate that a function is recursive.
return Used to indicate a value to provide as the result of a computation expression.
return! Used to indicate a computation expression that, when evaluated, provides the result of the containing computation expression.
select Used in query expressions to specify what fields or columns to extract. Note that this is a contextual keyword, which means that it is not actually a reserved word and it only acts like a keyword in appropriate context.
static Used to indicate a method or property that can be called without an instance of a type, or a value member that is shared among all instances of a type.
struct Used to declare a structure type.
Also used in generic parameter constraints.
Used for OCaml compatibility in module definitions.
then Used in conditional expressions.
Also used to perform side effects after object construction.
to Used in for loops to indicate a range.
true Used as a Boolean literal.
try Used to introduce a block of code that might generate an exception. Used together with with or finally.
type Used to declare a class, record, structure, discriminated union, enumeration type, unit of measure, or type abbreviation.
upcast Used to convert to a type that is higher in the inheritance chain.
use Used instead of let for values that require Dispose to be called to free resources.
use! Used instead of let! in asynchronous workflows and other computation expressions for values that require Dispose to be called to free resources.
val Used in a signature to indicate a value, or in a type to declare a member, in limited situations.
void Indicates the .NET void type. Used when interoperating with other .NET languages.
when Used for Boolean conditions (when guards) on pattern matches and to introduce a constraint clause for a generic type parameter.
while Introduces a looping construct.
with Used together with the match keyword in pattern matching expressions. Also used in object expressions, record copying expressions, and type extensions to introduce member definitions, and to introduce exception handlers.
yield Used in a sequence expression to produce a value for a sequence.
yield! Used in a computation expression to append the result of a given computation expression to a collection of results for the containing computation expression.

No comments:

Post a Comment