Skip to content

Instantly share code, notes, and snippets.

@mndrake
Created February 17, 2014 17:47
Show Gist options
  • Save mndrake/9055485 to your computer and use it in GitHub Desktop.
Save mndrake/9055485 to your computer and use it in GitHub Desktop.
intro to F#
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"language": "fsharp",
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"<H1>Introduction to F\\#</H1>"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"This will be an overview of basic F# syntax. \n",
"\n",
"To learn F# you can start by doing:\n",
"\n",
"* the introduction lessons at http://www.tryfsharp.org\n",
"\n",
"Then do some of the following:\n",
"\n",
"* Project Euler problems http://projecteuler.net \n",
"* Learn by testing https://github.com/ChrisMarinos/FSharpKoans\n",
"\n",
"Other resources:\n",
"\n",
"* F# reference on MSDN http://msdn.microsoft.com/en-us/library/dd233154.aspx"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Comments"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"// this is a comment\n",
"(* this is also a comment *)\n",
"(* this is\n",
" a\n",
" multiple line\n",
" comment *)"
],
"language": "python",
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"###Definitions and Type Inference\n",
"\n",
"Definitions are made using the ```let``` keyword. F# will infer the type based on the values."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"let a = 4\n",
"let b = 1.5\n",
"let c = \"hello\"\n",
"\n",
"a,b,c"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 17,
"text": [
"val it : int * float * string = (4, 1.5, \"hello\")"
]
}
],
"prompt_number": 17
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###Math Operators\n",
"\n",
"There are math keywords build into F#.\n"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"// exponential power\n",
"exp(1.0)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 26,
"text": [
"val it : float = 2.718281828"
]
}
],
"prompt_number": 26
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"// raise to a power for floats\n",
"3.0 ** 2.0"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 27,
"text": [
"val it : float = 9.0"
]
}
],
"prompt_number": 27
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"// raise to a power for integers\n",
"pown 3 2"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 36,
"text": [
"val it : int = 9"
]
}
],
"prompt_number": 36
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"// modulus\n",
"8%3"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 38,
"text": [
"val it : int = 2"
]
}
],
"prompt_number": 38
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"###Lists\n",
"\n",
"Lists are defined by brackets [ ]. Also common in F# are arrays [| |] and sequences seq { }. \n",
"\n",
"To define a sequence of numbers you can use ```start .. end``` or if the increment is other than one use ```start .. increment .. end```."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"[1..10]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 2,
"text": [
"val it : int list = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]"
]
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"[10..-1..1]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 13,
"text": [
"val it : int list = [10; 9; 8; 7; 6; 5; 4; 3; 2; 1]"
]
}
],
"prompt_number": 13
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"###Functions\n",
"\n",
"To create a named function the format is: \n",
"\n",
"```let``` *function_name* *arguments* = *function_body*"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"let f x = x + 1\n",
"\n",
"f 4"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 15,
"text": [
"val it : int = 5"
]
}
],
"prompt_number": 15
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"###Pipeline\n",
"\n",
"The pipeline operator ```|>``` is used to use the value on the left side of the operator as the input to the function on the right side of the operator. It is used the chain multiple steps together."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"let addOne x = x + 1\n",
"let timesTwo x = x * 2\n",
"\n",
"3 |> addOne |> timesTwo"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 14,
"text": [
"val it : int = 8"
]
}
],
"prompt_number": 14
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###Anonymous Functions\n",
"\n",
"Anonymous functions allow for the creation of \"throw away\" or \"helper\" functions. \n",
"\n",
"The have the syntax of : \n",
"```fun``` *arguments* -> *function_body*"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"4 |> fun x -> x+1"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 16,
"text": [
"val it : int = 5"
]
}
],
"prompt_number": 16
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### FSharp Collections\n",
"\n",
"http://msdn.microsoft.com/en-us/library/ee353413.aspx\n",
"\n",
"The F# collection modules have many useful methods that you will need to get familiar with. (List, Array, Seq, Array2D, Set, etc.) \n",
"\n",
"The following is just a sample of the methods available."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"// filter - filters any values that the condition is true\n",
"[1 .. 10] |> List.filter (fun n -> n%2=0)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 22,
"text": [
"val it : int list = [2; 4; 6; 8; 10]"
]
}
],
"prompt_number": 22
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"// map - maps a function to each element\n",
"[1 .. 5] |> List.map (fun n -> n*n)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 33,
"text": [
"val it : int list = [1; 4; 9; 16; 25]"
]
}
],
"prompt_number": 33
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"// sum - sums the collection, elements must have an add method\n",
"[1 .. 5] |> List.sum"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 34,
"text": [
"val it : int = 15"
]
}
],
"prompt_number": 34
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"// sumBy - applies a function to each member before summing\n",
"[1 .. 5] |> List.sumBy (fun n -> n*n)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 35,
"text": [
"val it : int = 55"
]
}
],
"prompt_number": 35
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"// Seq.unfold - returns a sequence generated by a function\n",
"Seq.unfold (fun (a,b) -> Some(a,(b,b+a))) (1,2)\n",
"|> Seq.take 5 // take first 5 items in sequence\n",
"|> Seq.toList // convert to a list"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 43,
"text": [
"val it : int list = [1; 2; 3; 5; 8]"
]
}
],
"prompt_number": 43
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"// infinite sequence of square integers\n",
"Seq.initInfinite (fun i -> i*i)\n",
"|> Seq.takeWhile (fun n -> n<100) // take all less than 100\n",
"|> Seq.sum // sum"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 2,
"text": [
"val it : int = 285"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"###Match\n",
"\n",
"The ```match``` keyword uses pattern matching and can be thought of a case or switch statement (only more powerful)."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"let isEven n =\n",
" match n with\n",
" |x when x%2=0 -> true\n",
" |_ -> false\n",
" \n",
"isEven 5"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 3,
"text": [
"val it : bool = false"
]
}
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Recursion\n",
"\n",
"The ```rec``` keyword is used to indicate that a function can be called recursively."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"let rec factorial_v1 n = \n",
" match n with\n",
" |0|1 -> 1I\n",
" |n -> bigint n * factorial_v1(n-1)\n",
" \n",
"List.init 5 factorial_v1"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 20,
"text": [
"val it : Numerics.BigInteger list = [1; 1; 2; 6; 24]"
]
}
],
"prompt_number": 20
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"// tail recursive version (generally preferred versus prior version)\n",
"let factorial_v2 n =\n",
" let rec loop x acc =\n",
" match x with\n",
" |0|1 -> acc * 1I\n",
" |n -> loop (x-1) (bigint n*acc)\n",
" loop n 1I\n",
" \n",
"List.init 5 factorial_v2"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 1,
"text": [
"val it : Numerics.BigInteger list = [1; 1; 2; 6; 24]"
]
}
],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment