f#

Using incomplete pattern matching as filter?

余生颓废 提交于 2019-12-07 04:56:17
问题 Suppose I have the following code: type Vehicle = | Car of string * int | Bike of string let xs = [ Car("family", 8); Bike("racing"); Car("sports", 2); Bike("chopper") ] I can filter above list using incomplete pattern matching in an imperative for loop like: > for Car(kind, _) in xs do > printfn "found %s" kind;; found family found sports val it : unit = () but it will cause a: warning FS0025: Incomplete pattern matches on this expression. For example, the value 'Bike (_)' may indicate a

Pattern Matching of Units of Measure in F#

余生长醉 提交于 2019-12-07 04:49:49
问题 This function: let convert (v: float<_>) = match v with | :? float<m> -> v / 0.1<m> | :? float<m/s> -> v / 0.2<m/s> | _ -> failwith "unknown" produces an error The type 'float<'u>' does not have any proper subtypes and cannot be used as the source of a type test or runtime coercion. Is there any way how to pattern match units of measure? 回答1: As @kvb explains in detail, the problem is that units of measure are a part of the type. This means that float<m> is different type than float<m/s> (and

How do you use (get values from keys, add items) Hashtables in F#

三世轮回 提交于 2019-12-07 04:44:07
问题 I would like to know how to use a System.Collections.Hashtable in F#. The reason it is a Hashtable is because I am referencing C# assemblies. How would I call the following methods? - Add - Get value from key I have not been able to find anything useful in Google about this. 回答1: As Mark points out, you can work with the Hashtable type directly from F# (just like with any other .NET type). The syntax for accessing indexers in F# is slightly different though: open System.Collections // 'new'

Why does F# Interactive behave differently than compiler with regards to immutable value definition?

こ雲淡風輕ζ 提交于 2019-12-07 04:22:58
问题 In reading John Palmer's answer to What is the difference between mutable values and immutable value redefinition?, John notes that This sort of redefinition will only work in fsi. In working with F# Interactive (fsi) I guess I subconsciously knew it, but never paid attention to it. Now that it is apparent, why the difference? More specifically please explain how the internals are different between fsi and the compiler such that this occurs by design or result of differences? If the answer

F# Function Composition with multiple input parameters

拜拜、爱过 提交于 2019-12-07 04:20:32
问题 I am new to F# and I recently discovered the function composition operator >> I understand the basic principle so that something like this is possible.... let Add1ToNum x = x +1 let Mul2ToNum y = y * 2 let FuncComp = Add1ToNum >> Mul2ToNum However, how would one handle composition when you have several functions that have a varying number of input parameters... for instance I would like to be able to do the following... let AddNums (x,y) = x+y let MulNums (x,y) = x*y let FuncComp = Add1 >>

Do F# observable events obviate, mediate, or are not related to the need for weak references?

こ雲淡風輕ζ 提交于 2019-12-07 03:59:34
问题 Since observables are typically IDisposable how does that change, if at all, the need to use weak references in event handlers, or any other event based memory leak/GC locked referencing? While my primary concern/need is for WPF I'm looking for the broader example and trying to understand where I may need weak references. F#'s Observable.add doesn't provide a way to unhook the event, so I'm thinking it's less likely to be a source of leaks. Sample code: type Notifier() = let propChanged = new

Using a F# portable library from a Windows Phone 8 app

此生再无相见时 提交于 2019-12-07 03:57:08
问题 I´m trying to use a portable F# library in my Windows Phone 8 app using Visual Studio 2013 but the compiler cannot resolve dependency to the FSharp.Core.dll version 3.3.1.0. I have tried to manually add a reference to the FSharp.Core.dll from C:\Program Files (x86)\Reference Assemblies\Microsoft\FSharp.NETPortable. Is F# portable library supported for Windows Phone 8? 回答1: What portable library type are you using? In VS2013, you have to use the "Portable Profile (Legacy)" project type, which

Understanding a change to protected/base member usage in F# 3.0

99封情书 提交于 2019-12-07 03:22:38
问题 F# 3.0 adds stricter checks for calls to base and protected members. I have something like the following abstract class in C# that has protected static helper methods to be used by derived classes. public abstract class Processor { public abstract void Process(); protected static void Helper(object arg) { } } In F#, one of those helper methods is passed as a first-class function: type DerivedProcessor() = inherit Processor() let init f = f () override x.Process() = init Processor.Helper It

F# charting example

僤鯓⒐⒋嵵緔 提交于 2019-12-07 02:47:09
问题 I would like to do some basic charting in F# using build in features or a free library. And I would be very very pleased with a very basic example of it, a pie chart if possible. Example data : [("John",34);("Sara",30);("Will",20);("Maria",16)] Where the ints are percentages to be represented in the pie. I have recently installed VSLab and though I find a lot of 3D examples, I am only looking for a simple pie chart... It is also fine to use excel features by the way, not free, but installed

Why does deep usage of the stack cause superlinear time behavior for a simple interpreter?

别来无恙 提交于 2019-12-07 02:42:28
问题 type Expr = | Lit of int | Add of Expr * Expr let rec intr = function | Lit _ as x -> x | Add(Lit a,Lit b) -> Lit <| a + b | Add(a,b) -> intr <| Add(intr a, intr b) let rec intr_cps x ret = match x with | Lit _ as x -> ret x | Add(Lit a,Lit b) -> Lit (a + b) |> ret | Add(a,b) -> intr_cps a <| fun a -> intr_cps b <| fun b -> intr_cps (Add(a, b)) ret let rec add n = if n > 1 then Add(Lit 1, add (n-1)) else Lit 1 open System.Threading let mem = 1024*1024*512 // ~536mb // It stack overflows