f#

Is it possible to make the following example fully polymorphic in F#?

不羁岁月 提交于 2019-12-05 18:05:44
type Mul = Mul with member inline __.Op(a: ^a,b: ^a) = a*b type Div = Div with member inline __.Op(a: ^a,b: ^a) = a/b type Add = Add with member inline __.Op(a: ^a,b: ^a) = a+b type Sub = Sub with member inline __.Op(a: ^a,b: ^a) = a-b let inline op x a b = (^a: (member Op: ^b * ^b -> ^b) x,a,b) let inline tup2 a b c d = op Mul a b, op Mul c d let inline tup2' f a b c d = op f a b, op f c d let a = tup2 1 2 3.0f 4.0f //let b = tup2' Mul 1 2 3.0f 4.0f //Gives a type error. I am wondering if there is a way to make the types do what I want in the example above or if I have finally reached the

How can I use WPF menus and dialog boxes in F#?

谁都会走 提交于 2019-12-05 18:03:52
I've been trying to find an example of using XAML and F# - without C# - to set up traditional menus and dialog boxes. Everything I can find online either uses C# or it is old, before the most recent versions of F# and .NET. Can anyone suggest an example I can look at? Thanks. When you try to learn WPF, you come across many C# examples based on "good old" code behind rather than MVVM or MVC. The following explains how to quickly create an F# WPF code behind application. Using this, it becomes easier to experiment with all those examples. Create an F# console application. Change the Output type

F# Discriminated Union - “downcasting” to subtype

≯℡__Kan透↙ 提交于 2019-12-05 18:03:17
I don't really know what the proper title for this question should be, but: I have a discriminated union called MyDiscriminatedUnion in F# : type MyDiscriminatedUnion = | Foo of Foo | Bar of Bar where Foo and Bar are record types: type Foo = { ... } type Bar = { ... } I created a value of union type Foo : let foo = Foo { ... } The compiler tells me that foo is of type MyDiscriminatedUnion . Then I want to pass foo into a function that expects type Foo, not MyDiscriminatedUnion . Therefore the compiler complains. How do I tell the compiler that foo is of type Foo ? I have tried: let foo:Foo

Recursive function vs recursive variable in F#

三世轮回 提交于 2019-12-05 17:56:23
The first method is OK. The second repeats constantly the same pair of numbers. It is quite obscure to me why... Could you point to the good direction ? module Normal = let rnd = new MersenneTwister() let sampleNormal = fun () -> let rec randomNormal() = let u1, u2 = rnd.NextDouble(),rnd.NextDouble() let r, theta= sqrt (-2. * (log u1)), 2. * System.Math.PI * u2 seq { yield r * sin theta; yield r * cos theta ; printfn "next";yield! randomNormal() } randomNormal() let sampleNormalBAD = fun () -> let rec randomNormal = let u1, u2 = rnd.NextDouble(),rnd.NextDouble() let r, theta= sqrt (-2. * (log

How to generate C#-friendly, .Net 4.0 compatible types using F# 3.0 type providers

无人久伴 提交于 2019-12-05 17:55:29
问题 I want to generate "strong" types based on "weakly" typed data sources, using the F# 3.0 type provider mechanism. The generated types must be accessible from C# clients in an environment where only .Net 4.0 is installed, but not .Net 4.5. If .Net 4.0 compatibility is not possible, we cannot use type providers in our current large-scale ERP project. So far, I have succeeded in creating MyGeneratedTypes.dll by following the tutorial on msdn (section "Providing Generated Types"), using the

Converting Haskell Polymorphic Cosine function to F#

眉间皱痕 提交于 2019-12-05 17:20:18
I'm trying to convert some Haskell code to F# but I'm having some trouble since Haskell is lazy by default and F# is not. I'm also still learning my way around F#. Below is a polymorphic cosine function in Haskell with pretty good performance. I want to try and keep the same or better performance parameters in F#. I would like to see a F# List version and a F# Seq version since the Seq version would be more like the lazy Haskell but the List version would probably perform better. Thanks for any help. Efficiency: number of arithmetic operations used proportional to number of terms in series

How to create record class with default constructor

和自甴很熟 提交于 2019-12-05 17:14:09
structures got default constructors like if I do type tagONEDEV_FlowRec = struct ....... end I can do new DeviceModel.tagONEDEV_FlowRec() but it doesn't work with this : let (<++|) device bytes size = let unmanagedPtr = Marshal.AllocHGlobal(size : int) Marshal.Copy( (bytes : byte array), 0, unmanagedPtr, size) Marshal.PtrToStructure(unmanagedPtr, (device : obj)) // Here Marshal.FreeHGlobal(unmanagedPtr) I need a record class here alike [<type:StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)>] type tagONEDEV_FlowRec = { mutable ....;} or type tagONEDEV_FlowRec = class .......

F# - how to group previous, this and next elements in circular Seq

孤人 提交于 2019-12-05 17:12:29
In F# How to best convert a finite Sequence-like seq [0; 1; 2; 3; 4] into a Sequence of tuples like seq [4,0,1 ; 0,1,2 ; 1,2,3 ; 2,3,4 ; 3,4,0] ? Addition: My Seq represents circular data. In this case the vertices of a closed polyline. I need the neighboring elements to compute the angle of each corner. I would do it like this: let neighbors xs = match Array.ofSeq xs with | [||] -> [||] | [|x|] -> [|x, x, x|] | xs -> let n = xs.Length [|yield xs.[n-1], xs.[0], xs.[1] for i=1 to n-2 do yield xs.[i-1], xs.[i], xs.[i+1] yield xs.[n-2], xs.[n-1], xs.[0]|] Comparisons are generally much faster

F#, Visual Studio 2017 and dotnet new

眉间皱痕 提交于 2019-12-05 17:09:30
To create a .NET class library from the command line, you can run the script dotnet new classlib Do that in a clean folder, and it will create a csproj file that can then be opened in Visual Studio 2017. However, run the script dotnet new classlib -lang f# in a clean folder, and the fsproj file that is subsequently create cannot be opened in Visual Studio 2017. The error message reads The imported project "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Sdks\FSharp.NET.Sdk\Sdk\Sdk.props" was not found. Confirm that the path in the declaration is correct, and that the

F# DataTable to SQL using SqlBulkCopy

北城余情 提交于 2019-12-05 17:00:21
I have an F# program that creates a DataTable , populates it with one row and then writes the data to SQL Server using bulk insert ( SqlBulkCopy ). Although it's working, I can't really figure out how to include a loop that will generate a number of list items / data rows which I can then insert in one statement, rather than having to bulk insert a single row at a time (which is the current case) here's my code: open System open System.Data open System.Data.SqlClient let lcpSqlConnection = new SqlConnection("<my-connection-string>") lcpSqlConnection.Open() let bulkLoadEsgData (conn