Define symbolic operators with F#

This post describes how we can define a custom operator with F#. Defining a new operator in F# is very straightforward. For instance, by using the following code, we can define “!” as an operator to compute factorial of an integer.

Running the following command in the interactive F# console computes the factorial for 10.

> !10;; 
val it : int = 3628800 
>

A symbolic operator can use any sequence of the following characters: ! % & * + - . / ? @ ^ |~. Note that, “:” can also be used in the character sequence of symbolic operation if and only if, it is not the first character in the operator.

Following is an example of modulo operator defined using F#:

Following outputs shows how to use it from F# interactive shell.

> 23 |%| 3;;
val it : int = 2
> 3 |%| 3;;
val it : int = 0
>
Advertisements

Eclipse PDE | Get all the Projects available in current Workspace

This post quickly describes how to get the list of projects available in the current Eclipse workspace. Eclipse PDE’s API provides the functionalities to query different meta-data relevant to the current Eclipse workspace. Using following code snippet, all the projects of the current workspace can be retrieved as an array of IProject …

Eclipse PDE’s API provides various functionalities to query different meta-data relevant to the currently active Eclipse workspace. Using following code snippet, all the projects of the current workspace can be retrieved as an array of IProject :

Further processing on this array can be performed e.g., to filter only open projects, as follows :

F# | Length of a List

The code snippets listed below defines a function to compute the length of a give list using F#. Note that these functions are also called polymorphic function, as they work with any type of list (as shown in the output).

A tail recursive implementation is outlined next.

Following is a more succinct implementation using List.Fold.

Output:

> length [];;
val it : int = 0
> length [1;2;3;4];;
val it : int = 4
> length ['a';'b'];;
val it : int = 2