f#

Is there any built-in function for human-readable F# quotations?

点点圈 提交于 2019-12-19 08:17:57
问题 When quoting <@ 1 + 1 @> I want "1 + 1" instead of "Call (None, Int32 op_Addition[Int32,Int32,Int32](Int32, Int32), [Value (1), Value (1)])" 回答1: You'll have to write it yourself. See the F# quotations visualizer code as a guide for transforming the quotations abstract syntax tree. 回答2: I have implemented a quotation decompiler as part of a larger open source project Unquote. It can decompile many simple F# quoted expressions as single-line non-light syntax strings (see the project's home

Piping another parameter into the line in F#

跟風遠走 提交于 2019-12-19 07:29:22
问题 Is piping parameter into line is working only for functions that accept one parameter? If we look at the example at Chris Smiths' page, // Using the Pipe-Forward operator (|>) let photosInMB_pipeforward = @"C:\Users\chrsmith\Pictures\" |> filesUnderFolder |> Seq.map fileInfo |> Seq.map fileSize |> Seq.fold (+) 0L |> bytesToMB where his filesUnderFolder function was expecting only rootFolder parameter, what if the function was expecting two parameters, i.e. let filesUnderFolder size rootFolder

Why can't a function with byref be converted directly to delegate?

岁酱吖の 提交于 2019-12-19 07:23:47
问题 Under normal circumstances, F# functions can be converted to delegates by calling new DelegateType and passing in the function as an argument. But when the delegate contains byref parameter, this is not possible directly. For example the code: type ActionByRef<'a> = delegate of 'a byref -> unit let f (x:double byref) = x <- 6.0 let x = ref 42.0 let d = new ActionByRef<_>(f) won't compile, giving the following error: This function value is being used to construct a delegate type whose

Pattern matching with guards vs if/else construct in F#

对着背影说爱祢 提交于 2019-12-19 06:34:26
问题 In ML-family languages, people tend to prefer pattern matching to if/else construct. In F#, using guards within pattern matching could easily replace if/else in many cases. For example, a simple delete1 function could be rewritten without using if/else (see delete2 ): let rec delete1 (a, xs) = match xs with | [] -> [] | x::xs' -> if x = a then xs' else x::delete1(a, xs') let rec delete2 (a, xs) = match xs with | [] -> [] | x::xs' when x = a -> xs' | x::xs' -> x::delete2(a, xs') Another

Writing CLS compliant code in F#

人盡茶涼 提交于 2019-12-19 06:26:11
问题 I am very new to F# and I started to write my functional wrapper on top of OpenGL. I also intend to use it to write a graphics engine which should have interop with all .Net languages. But it is hard to find information about which code constructs in F# are not CLS compliant. For example I already know of a few that are not CLS compliant: static type constrains tupleless functions probably 'T list and Seq maybe even union types How do I know what features of F# are not CLS compliant? 回答1: Use

Converting OCaml to F#: Differences between typing and type inference

混江龙づ霸主 提交于 2019-12-19 06:01:29
问题 In researching type inference differences between F# and OCaml I found they tended to focus on nominative vs. structural type system. Then I found Distinctive traits of functional programming languages which list typing and type inference as different traits. Since the trait article says OCaml and F# both use Damas-Milner type inference which I thought was a standard algorithm, i.e. an algorithm that does not allow for variations, how do the two traits relate? Is it that Damas-Milner is the

F#: removing duplicates from a seq is slow

拈花ヽ惹草 提交于 2019-12-19 06:01:04
问题 I am attempting to write a function that weeds out consecutive duplicates, as determined by a given equality function, from a seq<'a> but with a twist: I need the last duplicate from a run of duplicates to make it into the resulting sequence. For example, if I have a sequence [("a", 1); ("b", 2); ("b", 3); ("b", 4); ("c", 5)] , and I am using fun ((x1, y1),(x2, y2)) -> x1=x2 to check for equality, the result I want to see is [("a", 1); ("b", 4); ("c", 5)] . The point of this function is that

F# - public literal

旧巷老猫 提交于 2019-12-19 05:58:10
问题 Is there a way to define a public literal (public const in C#) on a type? Apparently let bindings in types must be private and the Literal attribute can't be applied to members. 回答1: Use the [< Literal >] attribute. This does the magic you are looking for. In addition, putting the literal attribute on a value such as a string enables it to be used within pattern matching. For example: // BAD - introduces new value named 'Name' let Name = "Chris Smith" match Console.ReadLine() with | Name ->

Converting F# Quotations into LINQ Expressions

坚强是说给别人听的谎言 提交于 2019-12-19 05:54:27
问题 I can convert a quotation of type Expr<'a -> 'b> to a Linq expression via the following snippet: /// Converts a F# Expression to a LINQ Lambda let toLambda (exp:Expr) = let linq = exp.ToLinqExpression() :?> MethodCallExpression linq.Arguments.[0] :?> LambdaExpression /// Converts a Lambda quotation into a Linq Lamba Expression with 1 parameter let ToLinq (exp : Expr<'a -> 'b>) = let lambda = toLambda exp Expression.Lambda<Func<'a, 'b>>(lambda.Body, lambda.Parameters) Now I want to convert a

Fractional power of units of measures in F#

心已入冬 提交于 2019-12-19 05:50:08
问题 Is it true to say that : there are no fractional power units in F# 回答1: In addition to what has already been said, the best resource for information about (not just) F# units of measure is Andrew Kennedy's PhD thesis, who actually designed F# units. He mentions fractional units: The most important decision is whether or not to allow fractional exponents of dimensions. The argument against them is philosophical: a quantity with a dimension such as M 1/2 makes no sense physically, and if such a