1 people like it.

sandc23102015.fsx

class on learning F#

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
67: 
68: 
69: 
70: 
71: 
72: 
73: 
74: 
75: 
76: 
77: 
78: 
79: 
80: 
81: 
82: 
/// LinearStudent: a first tutorial on programming the Simplex method in F#
/// F# is a functional-first programming language developed in the Cambridge Microsoft research lab
/// under the direction of a group of well-qualified researchers headed by Simon Peyton-Jones

let A2FM = "good class!"

// glorified calculator
2.*asin(1.0)
let pi = 3.141592654
sin(pi/2.0)

[|sin(3.*pi/2.0); cos(2.*pi/3.0); sin(2.*pi/3.); sqrt(3.)/2.|]

(fun y -> 2*y) (3+2)

// Create a matrix of floating point numbers
let matrix = [| [| 0.; 1.; 2.; 3. |]; [|4.0; 5.0; 6.0; 7.0|]; [|8.0; 9.0; 10.0; 11.0|]|]

// float converts an integer to a float
// and Array.map float r converts all the entries of the array r to floats

matrix.[0].[1]
// this takes the 2nd element of the 1st row of matrix.
// in F# arrays are indexed from 0 rather than 1.  Cope!

// pivotDivideMax: ensures the maximum entry in an array is 1
let inline pivotDivideMax l = Array.map (fun x -> x/(Array.max l)) l

// Array.map f builds a new array whose elements have all had function f applied to them
Array.map (fun x -> 2.*x ) matrix.[0]

matrix.[0]
// The old array is still there. This is functional (first) programming

// A better but more generic version without requiring the "inline" directive
// pivotDivideMaxHigher : div:('a -> 'a -> 'b) -> l:'a [] -> 'b []
let pivotDivideMaxHigher div l = Array.map (fun x -> div x (Array.max l)) l

// Test better version
pivotDivideMaxHigher (fun x y -> x/y) matrix.[1]

// F# uses a special syntax x <- v to change the value of mutable variables
matrix.[0] <- pivotDivideMaxHigher (fun x y -> x/y) matrix.[0]

// The entire first row of matrix has now been normalised
matrix

// we could -- with a bit of jiggery pokery -- define a generic division function ...
let inline myDiv (x:^a) (y:^a) = x/y
// test myDiv
myDiv 3 4
myDiv 3.0 4.0

// .... and use it to abbreviate pivot division actions
pivotDivideMaxHigher myDiv matrix.[1]
let inline pivotDivideMax2 l = pivotDivideMaxHigher myDiv l

matrix.[1] <- pivotDivideMax2 matrix.[1]
// The second row of matrix has been normalised as well
matrix

// rowOp applies a generic row operation to 2 rows
// rowOp : ('a -> 'b -> 'c) -> 'a [] -> 'b [] -> 'c []
let rowOp op r1 r2 = Array.map2 op r1 r2

// Compute r1 - 2 r2 as a new row
rowOp (fun x y -> x - 2.0*y) matrix.[0] matrix.[1]

// Note that rowOp works on any array, so columns work as well

// let badDiv x y = x/y // this does not have a general type
// badDiv 3 4 // this is ok
// badDiv 3.0 4.0 // but this is not ok

/// How to locate the _position_ of an element in an Array
(fun x -> x = 1.0) 2.0 // false
(fun x -> x = 1.0) 1.0 // true

let unit = Array.findIndex (fun x -> x = 1.0) matrix.[0] // find the (first) element equal to 1.0
matrix.[0].[unit]

/// Exercise: now find the position of the _maximum_ element in the 3rd row of matrix
val A2FM : string

Full name: Script.A2FM


 LinearStudent: a first tutorial on programming the Simplex method in F#
 F# is a functional-first programming language developed in the Cambridge Microsoft research lab
 under the direction of a group of well-qualified researchers headed by Simon Peyton-Jones
val asin : value:'T -> 'T (requires member Asin)

Full name: Microsoft.FSharp.Core.Operators.asin
val pi : float

Full name: Script.pi
val sin : value:'T -> 'T (requires member Sin)

Full name: Microsoft.FSharp.Core.Operators.sin
val cos : value:'T -> 'T (requires member Cos)

Full name: Microsoft.FSharp.Core.Operators.cos
val sqrt : value:'T -> 'U (requires member Sqrt)

Full name: Microsoft.FSharp.Core.Operators.sqrt
val y : int
val matrix : float [] []

Full name: Script.matrix
val pivotDivideMax : l:'a [] -> 'b [] (requires comparison and member ( / ))

Full name: Script.pivotDivideMax
val l : 'a [] (requires comparison and member ( / ))
module Array

from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> array:'T [] -> 'U []

Full name: Microsoft.FSharp.Collections.Array.map
val x : 'a (requires comparison and member ( / ))
val max : array:'T [] -> 'T (requires comparison)

Full name: Microsoft.FSharp.Collections.Array.max
val x : float
val pivotDivideMaxHigher : div:('a -> 'a -> 'b) -> l:'a [] -> 'b [] (requires comparison)

Full name: Script.pivotDivideMaxHigher
val div : ('a -> 'a -> 'b) (requires comparison)
val l : 'a [] (requires comparison)
val x : 'a (requires comparison)
val y : float
val myDiv : x:'a -> y:'a -> 'a0 (requires member ( / ))

Full name: Script.myDiv
val x : 'a (requires member ( / ))
val y : 'a (requires member ( / ))
val pivotDivideMax2 : l:'a [] -> 'b [] (requires member ( / ) and comparison)

Full name: Script.pivotDivideMax2
val l : 'a [] (requires member ( / ) and comparison)
val rowOp : op:('a -> 'b -> 'c) -> r1:'a [] -> r2:'b [] -> 'c []

Full name: Script.rowOp
val op : ('a -> 'b -> 'c)
val r1 : 'a []
val r2 : 'b []
val map2 : mapping:('T1 -> 'T2 -> 'U) -> array1:'T1 [] -> array2:'T2 [] -> 'U []

Full name: Microsoft.FSharp.Collections.Array.map2
Multiple items
val unit : int

Full name: Script.unit


 How to locate the _position_ of an element in an Array


--------------------
type unit = Unit

Full name: Microsoft.FSharp.Core.unit
val findIndex : predicate:('T -> bool) -> array:'T [] -> int

Full name: Microsoft.FSharp.Collections.Array.findIndex
Raw view Test code New version

More information

Link:http://fssnip.net/t1
Posted:8 years ago
Author:fairflow
Tags: tryfsharp