Friday, July 15, 2016

F# Data Types

F# Fundamentals | Pluralsight

Visual Studio 2015 -> View -> Other Windows -> F# Interactive

Primitive Types: no automatic type conversion
1.          // float, has a .
255uy       // byte
3.14M       // decimal
to convert data type use type name as a function name:
string 3.14 // convert to string
float "3.14"// convert to float
Literals (F#) @ MSDN
floating point - Difference between Decimal, Float and Double in .NET? - Stack Overflow
C# in Depth: Decimal floating point in .NET
decimal type is just another form of floating point number - but unlike float and double, the base used is 10; A decimal is stored in 128 bits

Boolean operations: &&, ||, not
String indexing:
"Hello".[1] // 'e'
"Hello".[1..2] // 'el'
"Hello".[1..] // 'ello'
String.init 3 (for i -> i * 10 |> string) // 01020 
Every F# statement evaluates to a value; no-value (void):  unit = ()
> printfn "Hello"
Hello
val it : unit = ()
Lists are immutable (linked list of same type); modify by creating new list from existing list
[]
// val it : 'a list = [] // empty list of any type
[1;2;3]  // is same as
1 :: [2;3] // head :: tail of the list, 'cons' operator ::
1::(2::(3::[])) // same thing
[1..3] // list range
// val it : int list [1; 2; 3]
[1..2..6] // increment 2
// val it : int list [1; 3; 5]
[1;2] @ [3;4] // list concatenation to a single list
// val it : int list = [1;2;3;4]
List comprehensions:
[for x in 1..3 do yield 2*x]
// val it : int list = [2;4;6]
[for x in 1..3 -> 2*x] // same thing, briefer syntax
[for x in 1..2 do
 for y in 1..2 do
     yield (x,y)] // result is a list of tuples
// val it : (int * int) list = [(1;1);(1;2);(2;1);(2;2)]
Arrays:
// val it : int list = [1;2;3;4]





No comments: