f#

How to get the name of function argument in F#?

扶醉桌前 提交于 2019-12-10 14:22:55
问题 Can I write a function which returns the name of the function given as the argument? let funName f: string = // returns the name of f. For example, if I pass printfn as an argument to funName, it returns "printfn". > funName printfn;; val it : string = "printfn" EDIT: I wanted to write a function doc which returns the XML documentation associated with the given function. let doc f = // returns the XML documentation of the function `f`. To retrieve the summary of the function using something

How to define a fmap on a record structure with F#

巧了我就是萌 提交于 2019-12-10 14:21:34
问题 is there a possibility to create an fmap for records so that I can apply the same function to record fields of similar bur different types Let say I have a record field type Item and a record X and function transform type Item<'a, 'b> = Item of 'a * 'b let transform (i: Item<'a, 'b>) : Item<'a, string> = let (Item (x, y)) = i Item (x, sprintf "%A" y) type X<'a> = { y: Item<'a, int> z: Item<'a, bool> } with member inline this.fmap(f) = { y = f this.y z = f this.z } now the line z = f this.z

Converting OCaml to F#: Is there a simple way to simulate OCaml top-level #trace in F#

泪湿孤枕 提交于 2019-12-10 14:20:09
问题 I am converting several modules based on OCaml to F#. I have the code converted and running in F#, however the result of the final function in F# is not the same as the result of the final function in OCaml. So obviously I have to follow the function calls to figure out which function is returning the wrong result. OCaml has a nice top-level directive for tracing the input and output of a function, i.e. #trace . I have searched F#'s debug and trace methods and the closest I get is to

Square Root for Bigint in F#

妖精的绣舞 提交于 2019-12-10 14:19:39
问题 Is there a way to get the square root of a big integer? I am dealing with numbers that are much too large for int64 to handle so bigint is a must. I was considering the idea of implementing it myself using the Babylonian method, but want to know if there are any built in functions for this first. Thanks in advance. 回答1: You can use newton's method on any scalar. http://en.wikipedia.org/wiki/Newton%27s_method -- MarkusQ P.S. See also http://en.wikipedia.org/wiki/Methods_of_computing_square

base CLI library 'mscorlib' is binary-incompatible with the referenced F# core library

邮差的信 提交于 2019-12-10 14:03:59
问题 I've built a website that takes code snippets and compiles them and runs them. However, F# is broken on the server. Whenever I try to compile F# code I get the following error message: The referenced or default base CLI library 'mscorlib' is binary-incompatible with the referenced F# core library 'C:\Program Files (x86)\Microsoft F#\v4.0\FSharp.Core.dll'. Consider recompiling the library or making an explicit reference to a version of this library that matches the CLI version you are using.

How does F# compile functions that can take multiple different parameter types into IL?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 13:59:24
问题 I know virtually nothing about F#. I don’t even know the syntax, so I can’t give examples. It was mentioned in a comment thread that F# can declare functions that can take parameters of multiple possible types, for example a string or an integer. This would be similar to method overloads in C#: public void Method(string str) { /* ... */ } public void Method(int integer) { /* ... */ } However, in CIL you cannot declare a delegate of this form. Each delegate must have a single, specific list of

Why do I have to wrap an Async<T> into another async workflow and let! it?

拜拜、爱过 提交于 2019-12-10 13:58:35
问题 I'm trying to understand async workflows in F# but I found one part that I really don't understand. The following code works fine: let asynWorkflow = async{ let! result = Stream.TryOpenAsync(partition) |> Async.AwaitTask return result } let stream = Async.RunSynchronously asynWorkflow |> fun openResult -> if openResult.Found then openResult.Stream else Stream(partition) I define a async workflow where TryOpenAsync returns a Task<StreamOpenResult> type. I convert it to Async<StreamOpenResult>

How to do a Applicative in F#?

我只是一个虾纸丫 提交于 2019-12-10 13:56:43
问题 Given the following type and member function type Result<'TSuccess, 'TError> = | Success of 'TSuccess | Error of 'TError list with member this.apply fn = match (fn, this) with | Success(f), Success(x) -> Success(f x) | Error(e), Success(_) -> Error(e) | Success(_), Error(e) -> Error(e) | Error(e1), Error(e2) -> Error(List.concat [e1;e2]) and the following inline function let inline (<*>) (f: ^A) (t:^A) = let apply' = (^A : (member apply : ^A -> ^A) (t, f)) apply' And this call site let y () =

Getting compile error on provided type

我怕爱的太早我们不能终老 提交于 2019-12-10 13:48:39
问题 I'm working on a TypeProvider that reads an XSD file and provides a type for each type defined in the XSD. However I have a problem in the below code type schema = XmlProviders.Schema<"file.xsd"> type Bazzer = { Sum : XmlProviders.bar } on the last line I get a compilation error saying that XmlProviders.bar does not exist. The implementation of how I define the types are as follows let defineType (xType : XElement) = let name = xType.Attribute(XName.Get "name").Value let t =

Performance of sprintf vs String.Format [duplicate]

狂风中的少年 提交于 2019-12-10 13:46:05
问题 This question already has answers here : Why is printf in F# so slow? (4 answers) Closed 6 years ago . I was comparing the performance of sprintf usages and am a little troubled by what I saw. I tested the following 4 methods, passing an instance of ClassWithToString in to each (except for PrintInt, which received the actual integer value). type ClassWithToString() = member this.X = 42 override this.ToString() = this.X.ToString() let Print item : string = sprintf "%A" item let PrintInt item: