(12/4 + 5 + 7) * 4 - 18 let numbers = [0 .. 10] numbers // Take the numbers from 2nd index to the 5th numbers.[2..5] numbers.[3] let sampleFunction x = 2*x*x - 5*x + 3 let sampleFunction' (x: int) = 2*x*x - 5*x + 3 sampleFunction 5 sampleFunction' 12 let negate x = -x let square x = x * x let print x = printfn "The number is %d" x let squareNegateThenPrint x = print (negate (square x)) squareNegateThenPrint 5 // Redefine the function with pipelines let squareNegateThenPrint x = x |> square |> negate |> print squareNegateThenPrint 5 let s1 = "Hello" let s2 = "World" s1 + ", " + s2 + "!" """A triple-quoted string can contain quotes "like this" anywhere within it""" (1, "fred", Math.PI) struct (1, Math.PI) [0 .. 10] let thisYear = DateTime.Now.Year let fridays = [ for month in 1 .. 10 do for day in 1 .. DateTime.DaysInMonth(thisYear, month) do let date = DateTime(thisYear, month, day) if date.DayOfWeek = DayOfWeek.Friday then date.ToShortDateString() ] // Get the first five fridays of this year fridays |> List.take 5 fridays.[..4] let firstTwoHundred = [| 1 .. 200 |] firstTwoHundred.[197..] // Filter the previous list of numbers and sum their squares. firstTwoHundred |> Array.filter (fun x -> x % 3 = 0) |> Array.sumBy (fun x -> x * x) type ContactCard = { Name: string Phone: string ZipCode: string } // Create a new record { Name = "Alf"; Phone = "(555) 555-5555"; ZipCode = "90210" } let alf = { Name = "Alf"; Phone = "(555) 555-5555"; ZipCode = "90210" } alf.Phone // Create another record let ralph = { Name = "Ralph"; Phone = "(123) 456-7890"; ZipCode = "90210" } // Check if they're equal alf = ralph let showContactCard contact = contact.Name + " - Phone: " + contact.Phone + ", Zip: " + contact.ZipCode showContactCard alf type Shape = | Rectangle of width: float * length: float | Circle of radius: float | Prism of width: float * height: float * faces: int let rect = Rectangle(length = 1.3, width = 10.0) let circ = Circle (1.0) let prism = Prism(width = 5.0, height = 2.0, faces = 3) prism let height shape = match shape with | Rectangle(width = h) -> h | Circle(radius = r) -> 2.0 * r | Prism(height = h) -> h let rectHeight = height rect let circHeight = height circ let prismHeight = height prism printfn "rect is %0.1f, circ is %0.1f, and prism is %0.1f" rectHeight circHeight prismHeight // See if x is a multiple of n let isPrimeMultiple n x = x = n || x % n <> 0 // Process lists recursively. // '[]' means the empty list. // 'head' is an item in the list. // 'tail' is the rest of the list after 'head'. let rec removeMultiples ns xs = match ns with | [] -> xs | head :: tail -> xs |> List.filter (isPrimeMultiple head) |> removeMultiples tail let getPrimesUpTo n = let max = int (sqrt (float n)) removeMultiples [2 .. max] [1 .. n] // Primes up to 25 getPrimesUpTo 25 let keepIfPositive a = if a > 0 then Some a else None keepIfPositive 12 let rec tryFindMatch predicate lst = match lst with | [] -> None | head :: tail -> if predicate head then Some head else tryFindMatch predicate tail let greaterThan100 x = x > 100 tryFindMatch greaterThan100 [25; 50; 100; 150; 200] #!time let bigArray = [| 0 .. 100_000 |] let rec fibonacci n = if n <= 2 then n else fibonacci (n-1) + fibonacci (n-2) // We'll use the '%A' print formatter for F# constructs for these results, since they are enormous let results = bigArray |> Array.Parallel.map (fun n -> fibonacci (n % 25)) printfn "%A" results #!time // Restrict the number of threads to a max of 25 let nThreads = min 25 Environment.ProcessorCount Array.Parallel.init nThreads fibonacci