From: Greg Burri Date: Sun, 23 Nov 2014 15:03:46 +0000 (+0100) Subject: First commit. X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=ac61fd49bf7897adbb18d2ae0f3bd843311a4ccf;p=fsharp-ref.git First commit. --- ac61fd49bf7897adbb18d2ae0f3bd843311a4ccf diff --git a/FSharpRef.sln b/FSharpRef.sln new file mode 100644 index 0000000..2e89cee --- /dev/null +++ b/FSharpRef.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.21005.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "FSharpRef", "FSharpRef\FSharpRef.fsproj", "{CFF39041-0222-46AD-BF2A-6C93955DD06E}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {CFF39041-0222-46AD-BF2A-6C93955DD06E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CFF39041-0222-46AD-BF2A-6C93955DD06E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CFF39041-0222-46AD-BF2A-6C93955DD06E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CFF39041-0222-46AD-BF2A-6C93955DD06E}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/FSharpRef/App.config b/FSharpRef/App.config new file mode 100644 index 0000000..c9bdc98 --- /dev/null +++ b/FSharpRef/App.config @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/FSharpRef/FSharpRef.fsproj b/FSharpRef/FSharpRef.fsproj new file mode 100644 index 0000000..cd169ed --- /dev/null +++ b/FSharpRef/FSharpRef.fsproj @@ -0,0 +1,77 @@ + + + + + Debug + AnyCPU + 2.0 + cff39041-0222-46ad-bf2a-6c93955dd06e + Exe + FSharpRef + FSharpRef + v4.5 + FSharpRef + 4.3.0.0 + + + true + full + false + false + bin\Debug\ + DEBUG;TRACE + 3 + AnyCPU + bin\Debug\FSharpRef.XML + true + + + pdbonly + true + true + bin\Release\ + TRACE + 3 + AnyCPU + bin\Release\FSharpRef.XML + true + + + + True + + + + + + + + + + + + + + 11 + + + + + $(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets + + + + + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\FSharp\Microsoft.FSharp.Targets + + + + + + \ No newline at end of file diff --git a/FSharpRef/Functional.fs b/FSharpRef/Functional.fs new file mode 100644 index 0000000..424dc35 --- /dev/null +++ b/FSharpRef/Functional.fs @@ -0,0 +1,115 @@ +module Functional + +open System.IO +open System.Linq + +// For dynmic typing. To print anything in our case. +let inline print a = printfn "%A" a + +// Write something on a file. +let appendFile (filename: string) (text: string) = + use sw = new StreamWriter(filename, true) + sw.WriteLine(text) + +// Recursive function. +let rec fac n = + if n <= 1 then 1 + else n * fac (n - 1) + +// Mutual recursive functions. +let rec isEven n = + if n = 0 then true + elif n = 1 then false + else isOdd (n - 1) +and isOdd n = + if n = 0 then false + elif n = 1 then true + else isEven (n - 1) + +// Algebraic data type. +type Suit = + | Diamond + | Spade + | Heart + | Club + +type PlayingCard = + | Ace of Suit + | King of Suit + | Queen of Suit + | Jack of Suit + | NumCard of int * Suit + member this.Value = // Property. + match this with + | Ace (_) -> 14 + | King (_) -> 13 + | Queen (_) -> 12 + | Jack (_) -> 11 + | NumCard (n, _) when n >= 2 && n <= 10 -> n + | NumCard (_) -> failwith "Card has invalid value!" + +let deckOfCards = + [ + for suit in [ Spade; Club; Heart; Diamond ] do + yield Ace(suit) + yield King(suit) + yield Queen(suit) + yield Jack(suit) + for n in [2 .. 10] do + yield NumCard(n, suit) + ] + +// Records. +type Person = { First: string; Last: string; Age: int } +let steve = { First = "Steve"; Last = "Holt"; Age = 17 } +let steves'twin = { steve with First = "Paul" } + +// Queries. Should be used with SQL or other external data sources. +let youngPersonNames persons = + query { + for p in persons do + where (p.Age < 15) + select p.First + } + +// Infinite lazy sequence. +let allPositiveInts = + seq { + for i in 1 .. System.Int32.MaxValue do + yield i + } +let rec fibs x y = + seq { + yield x + yield! fibs y (x + y) + } +let testYieldBang = + seq { + yield 10 + yield! [1; 2; 3] // Yields a collection. + } +// Generator. +let testUnfold = + Seq.unfold (fun a -> if a > 10 then None else Some(10 * a, a + 1)) 1 + +// Functions composition. +let SizeOfFolder = + let getFiles folder = + Directory.GetFiles(folder, "*.*", SearchOption.AllDirectories) + getFiles + >> Array.map (fun file -> (new FileInfo(file)).Length) + >> Array.sum + +// Matches literals. +[] +let Bill = "Bill Gates" // Must begin with a capital! +let greet name = + match name with + | Bill -> "Hello rich Bill!" + | name -> sprintf "Hello poor %s" name + +// Matches tuples. +let testXor x y = + match x, y with + | a, b when a <> b -> true + | _ -> false diff --git a/FSharpRef/Main.fs b/FSharpRef/Main.fs new file mode 100644 index 0000000..d070829 --- /dev/null +++ b/FSharpRef/Main.fs @@ -0,0 +1,28 @@ +module Main + +open System.Linq +open Functional + +[] +let main args = + appendFile "test.txt" "Pouet" + printfn "Size of folder: %A" (SizeOfFolder ".") + print "youpi" + print (isEven (fac 4)) + // + print steve.First + printfn "Young persons: " + (youngPersonNames (Queryable.AsQueryable [steve ; steves'twin ; { First = "Paul"; Last = "Atreides"; Age = 11 }])) |> Seq.iter (fun name -> printfn " name: %s" name) + // + print <| Seq.take 10 allPositiveInts + print <| Seq.take 10 (fibs 1 1) + // + print testYieldBang + print testUnfold + // + print <| greet Bill + print <| greet "Paul" + // + print <| testXor 1 2 + print <| testXor 3 3 + 0 diff --git a/FSharpRef/Object.fs b/FSharpRef/Object.fs new file mode 100644 index 0000000..c89cd72 --- /dev/null +++ b/FSharpRef/Object.fs @@ -0,0 +1,2 @@ +module Object +