f#

Confused with F# List.nth's argument order

本秂侑毒 提交于 2019-12-10 13:15:09
问题 List.nth is 'T list -> int -> 'T , rather than the standard int -> 'T list -> 'T like Seq.nth . This makes pipeline somewhat awkward. Is there something behind the scene? I don't know why. 回答1: Might be for ocaml compatibility (or just laziness), but without deeper reasons concerning the implementation itself. 回答2: This signature allows you to curry the function with respect to a given list. This allows you to keep a List.nth someList stored, and use it to get the nth element of the list

Is it possible to use System.Type as static parameter in F# type provider?

故事扮演 提交于 2019-12-10 13:13:07
问题 I was wondering is it possible to use System.Type as the static parameter in F# type provider, so that I can write something like: type HelperType = HelperProvider<typeof<int>> The idea is, is it possible to let type provider generating some helper type based on some .NET type. 回答1: No, type provider parameters can only be of primitive types (like int and string ). The best you can do is to take the type name as a string: type HelperType = HelperProvider<"int"> This will do the trick for

Why does the argument type of arithmetic operators default to int?

随声附和 提交于 2019-12-10 13:12:17
问题 I am new to F#, and I was surprised to find that the type of f x y = x + y is actually int -> int -> int . Appearently, this is due to some performance trade-off. But why is this actually necessary? Why not just infer the type to be 'a -> 'a -> 'a or something similar? It seems to work for comparison: the type of g x y = x < y is x:'a -> y:'a -> bool when 'a : comparison . Why not for arithmetic operators as well? Couldn't the compiler statically infer the specific primitive types from the

How to create a virtual record field for Entity Framework lazy loading

久未见 提交于 2019-12-10 13:10:09
问题 I'm trying to define some models that I use in Entity Framework 6. I would like to use F# records. If I put [<CLIMutable>] above the record definition and use ICollection<'a> for, well, the collections. EF works fine. I can store and retrieve the records. However for EF to do lazy loading I need to make the record field containing the collection virtual. I can make a property member virtual by making it abstract and providing a default. Records are not classes. Is it possible to make a record

f# sequence of running total

こ雲淡風輕ζ 提交于 2019-12-10 13:09:31
问题 Ok, this looks like it should be easy, but I'm just not getting it. If I have a sequence of numbers, how do I generate a new sequence made up of the running totals? eg for a sequence [1;2;3;4], I want to map it to [1;3;6;10]. In a suitably functional way. 回答1: Use List.scan: let runningTotal = List.scan (+) 0 >> List.tail [1; 2; 3; 4] |> runningTotal |> printfn "%A" Seq.scan-based implementation: let runningTotal seq' = (Seq.head seq', Seq.skip 1 seq') ||> Seq.scan (+) { 1..4 } |>

Unable to create F# Android Project - Visual Studio 2015 Xamarin Community

你离开我真会死。 提交于 2019-12-10 13:04:34
问题 I'm following the dialogue options to create a new F# Android Blank Activity in the visual studio community edition from the Xamarin download page and am not having much luck. C# android projects seem to work just fine. This is the visual studio 2015 release put out just after the Xamarin open source announcement. Once the project is created what looks to be an intro page appears for a brief moment before disappearing. The project structure was loaded in the solution explorer during the time

What's some simple F# code that generates the .tail IL instruction?

可紊 提交于 2019-12-10 13:01:59
问题 I'd like to see the .tail IL instruction, but the simple recursive functions using tail calls that I've been writing are apparently optimized into loops. I'm actually guessing on this, as I'm not entirely sure what a loop looks like in Reflector. I definitely don't see any .tail opcodes though. I have "Generate tail calls" checked in my project's properties. I've also tried both Debug and Release builds in Reflector. The code I used is from Programming F# by Chris Smith, page 190: let

Apply attribute to return value - In F#

为君一笑 提交于 2019-12-10 12:57:53
问题 in C# it is possible to apply an attribute to the return of a method: [return: DynamicAttribute] public object Xyz() { return new ExpandoObject(); } Is this possible in F#? Background : I would like a method of a library written in F# which will be consumed in a language like C# to return "dynamic" from a method. If I look into a similarly defined method compiled from C# it seems like the return type is object and the DynamicAttribute is applied to the return value of the object. 回答1: Sure,

What is the purpose of flexible type annotation in F#?

走远了吗. 提交于 2019-12-10 12:57:15
问题 I'm studying F# and I don't understand the purpose of flexible types, or better, I can't understand the difference between writing this: set TextOfControl (c : Control) s = c.Text <- s and writing this: set TextOfControl (c : 'T when 'T :> Control) s = c.Text <- s where Control is the System.Windows.Forms.Control class. 回答1: There is no difference in your example. If return types are constrained, you start seeing the difference: let setText (c: Control) s = c.Text <- s; c let setTextGeneric

F# Records: Fields With Identical Names

别说谁变了你拦得住时间么 提交于 2019-12-10 12:54:07
问题 Please consider the following record definitions: type A = { F1 : int; F2 : int } type B = { F1 : int; F3 : int } // error FS0656: This record contains fields from inconsistent types let a1 = { F1 = 1; F2 = 2 } // this works let a2 = { A.F1 = 1; F2 = 2 } I don't understand, why a1 results in an error. All the examples I could find of why you have to do it the a2-way assume that all the field-names in A and B have the same name - which is of course ambiguous, but shouldn't A and B be