f#

F# Change element in list and return full new list

大憨熊 提交于 2019-12-05 12:45:13
I have a list of type (string * (int * int)) list . I want to be able to search through the list, finding the right element by it's string identifier, do a calculation on one of the ints , and then return the full, modified list. Example: Given a list let st = [("a1",(100,10)); ("a2",(50,20)); ("a3",(25,40))] I'm trying to make a function which gets one of the elements and subtracts number from one of the ints in the tuple. get ("a2",10) st //Expected result: st' = [("a1",(100,10)); ("a2",(40,20)); ("a3",(25,40))] I feel I'm almost there, but am a little stuck with the following function: let

F#: Using INotifyPropertyChanged for data binding

∥☆過路亽.° 提交于 2019-12-05 12:26:02
问题 How would one implement INotifyPropertyChanged for use in an F# type? Thanks! 回答1: It's basically the same as in any other language: open System.ComponentModel type MyType() = let ev = new Event<_,_>() let mutable str = "" member x.StringProp with get() = str and set(str') = str <- str' ev.Trigger(x, PropertyChangedEventArgs("StringProp")) interface INotifyPropertyChanged with [<CLIEvent>] member x.PropertyChanged = ev.Publish 回答2: https://github.com/Fody/PropertyChanged https://github.com

How to compile a .fs file into a .exe?

陌路散爱 提交于 2019-12-05 12:25:05
I do not find a way to compile a simple file (.fs) to a .exe. I tried this example and it does not work: In the file dolphin.fs: let longBeaked = "Delphinus capensis" let shortBeaked = "Delphinus delphis" let dolphins = [ longBeaked; shortBeaked ] printfn "Known Dolphins: %A" dolphins You can now compile this code to an EXE as shown here: C:\fsharp> fsc dolphins.fs C:\fsharp> dir dolphins.exe But when I do this an error occurs on the ":" from "C:\fsharp" This seems a bit wrong - aside from the fact that you're not supposed to type the first bit (as explained by svick), you probably also need

How to make a function to return really different types in fsharp?

青春壹個敷衍的年華 提交于 2019-12-05 12:19:03
Assume that there is a third-party library written in FSharp, it contains several generic classes, for example as follows: type FirstType<'a> has method DoWork , that accepts: first param of type FirstType<'a> , second param is a function of type ('a -> 'b) DoWork method return type is FirstType<'b> type SecondType<'a> has method DoWork , that accepts: first param of type SecondType<'a> , second param is a function of type ('a -> 'b) DoWork method return type is SecondType<'b> type ThirdType <'a> has method DoWork , that accepts: first param of type ThirdType <'a>, second param is a function

Inconsistent processing of generic ParamArray argument in F#

寵の児 提交于 2019-12-05 12:15:27
Being spotted in context of this question this seemingly inconsistent behavior can be reproduced as following both in F#2.0 and F#3.0 RC: type Heterogeneous = static member Echo([<ParamArray>] args: Object[]) = args type Generic = static member Echo<'T>([<ParamArray>] args: 'T[]) = args Usage: Returns: Heterogeneous.Echo 0 // [|0|] OK Generic.Echo 0 // [|0|] OK Heterogeneous.Echo (0,1) // [|0; 1|] OK Generic.Echo (0,1) // [|0; 1|] OK Heterogeneous.Echo [|0|] // [|[|0|]|] OK? Generic.Echo [|0|] // [|0|] OOPS!! Heterogeneous.Echo ([|0|],[|1|])) // [|[|0|]; [|1|]|] OK Generic.Echo ([|0|],[|1|]))

FSharp.Core for Windows Phone 7.1 and F# 3.0

試著忘記壹切 提交于 2019-12-05 11:04:08
问题 The F# 2.0 distribution had a version of FSharp.Core compiled for WindowsPhone 7.1/Silverlight 4, but F# 3.0 doesn't, and the portable version only supports Silverlight 5 or Windows Phone 8. Has anyone been able to compile a version of FSharp.Core for F# 3.0 targeting Silverlight 4 or Windows Phone 7.1 from source? What are the defines required? Edit: On the fsharp compiler github repo, there's a target named portable-net4+sl4+wp71+win8, but in reality that's portable class library Profile47,

.NET // vs /// Comments convention

筅森魡賤 提交于 2019-12-05 10:54:24
问题 I am just checking out F#, so apologies if this is a silly question, but in the VS2008 F# CTP 1.9.6.2 'Tutorial' project, both // and /// are used for commenting code. Is there a functional difference between the two slash vs three slash commenting, or is it convention (as it appears in the tutorial code) to comment a function with /// and use // for everything else? 回答1: Using three slashes (///) is a C# convention for XML Documentation Comments that is adopted by F# as well. 回答2: /// is for

looping through F# record like Javascript object

扶醉桌前 提交于 2019-12-05 10:40:55
In javascript, I can access every property of an object with a simple for loop as follows var myObj = {x:1, y:2}; var i, sum=0; for(i in myObj) sum = sum + myObj[i]; I am wondering if I can do similar thing with F#. type MyObj = {x:int; y:int} let myObj = {x=1; y=2} let allValues:seq<int> = allPropertyValuesIn myObj //How do I implement allPropertyValuesIn let sum = allValues |> Seq.fold (+) 0 Thank you for your input Edit to clarify why I want to do such thing I am working on an XML file generator. The input is rows read from Database, and the xsd is predefined. Lets say I have a "Product"

How would you implement a beta-reduction function in F#?

最后都变了- 提交于 2019-12-05 10:39:31
I am writing a lambda calculus in F#, but I am stuck on implementing the beta-reduction (substituting formal parameters with the actual parameters). (lambda x.e)f --> e[f/x] example of usage: (lambda n. n*2+3) 7 --> (n*2+3)[7/n] --> 7*2+3 So I'd love to hear some suggestions in regards to how others might go about this. Any ideas would be greatly appreciated. Thanks! Assuming your representation of an expression looks like type expression = App of expression * expression | Lambda of ident * expression (* ... *) , you have a function subst (x:ident) (e1:expression) (e2:expression) : expression

Create an instance of Action<'T> using reflection

℡╲_俬逩灬. 提交于 2019-12-05 10:31:45
How would I create an instance of Action<'T> using reflection? Here's what I have: let makeAction (typ:Type) (f:'T -> unit) = let actionType = typedefof<Action<_>>.MakeGenericType(typ) let converter = FSharpFunc.ToConverter(f) Delegate.CreateDelegate(actionType, converter.Method) which barfs with: System.ArgumentException: Error binding to target method. at System.Delegate.CreateDelegate(Type type, MethodInfo method, Boolean throwOnBindFailure) 'T is an interface, which typ implements. I think there are two problems. The first one is that you need to call CreateDelegate overload that takes