f#

what is use cases of F# explicit type parameters?

此生再无相见时 提交于 2019-12-06 00:33:05
As I know, explicit type parameters in value definitions is a one way to overcome "value restriction" problem. Is there another cases when I need to use them? Upd : I mean "explicitly generic constructs", where type parameter is enclosed in angle brackets, i.e. let f<'T> x = x This would likely be rare, but when you want to prevent further generalization (§14.6.7): Explicit type parameter definitions on value and member definitions can affect the process of type inference and generalization. In particular, a declaration that includes explicit generic parameters will not be generalized beyond

Re-implementing List.map in OCaml/F# with correct side effect order?

假如想象 提交于 2019-12-06 00:29:09
问题 According to this previous answer You could implement List.map like this: let rec map project = function | [] -> [] | head :: tail -> project head :: map project tail ;; but instead, it is implemented like this: let rec map project = function | [] -> [] | head :: tail -> let result = project head in result :: map project tail ;; They say that it is done this way to make sure the projection function is called in the expected order in case it has side effects, e.g. map print_int [1;2;3] ;;

Is there an equivalent to the F# Seq.windowed in C#?

本小妞迷上赌 提交于 2019-12-05 23:48:14
问题 I am working on some C# code dealing with problems like moving averages, where I often need to take a List / IEnumerable and work on chunks of consecutive data. The F# Seq module has a great function, windowed, which taking in a Sequence, returns a sequence of chunks of consecutive elements. Does C# have an equivalent function out-of-the-box with LINQ? 回答1: You can always just call SeqModule.Windowed from C#, you just need to reference FSharp.Core.Dll . The function names are also slightly

How to properly overload global (+) and (*) without breaking support for other types

荒凉一梦 提交于 2019-12-05 23:40:28
I have imported a vector math library, and would like to add my own (*) and (+) operators while preserving the existing operators for basic int and float. I have tried the following: let inline (*) (x : float) (y : Vector) = y.Multiply(x) let inline (*) (x : Vector) (y : float) = x.Multiply(y) let inline (+) (x : Vector) (y : Vector) = x.Add(y) Which has two problems: It seems to remove int + int and int * int , and The 2nd line (which is intended to complete commutativity) does not compile because it is a "duplicate definition". How can I go about defining some commutative operators on my

F# type definition with expression

梦想与她 提交于 2019-12-05 23:29:34
问题 Is it possible to express something like this: type id = int > 0 I know its not possible to do statically, since this would mean F# has dependent types. In C# I'm used to do this sort of thing with code contracts and get a runtime enforcement. I'm looking for something similiar here. Thanks EDIT: Thank you for all the answers which have various pros and cons. At the monent I'm only using a small subset of F#, a subset of the ocaml core that lends itself easily to program proofs. So no classes

Rewrite some C# generic code into F#

家住魔仙堡 提交于 2019-12-05 22:41:45
问题 I'm trying to rewrite generic code like this (C#): U Upcast<T, U>(T x) where T : U { return x; } In F#: let ucast<'T, 'U when 'T :> 'U> (x: 'T) = x :> 'U But F# constraint solving works different than C# and compiler outputs a bunch of typing errors: error FS0698: Invalid constraint: the type used for the constraint is sealed, which means the constraint could only be satisfied by at most one solution warning FS0064: This construct causes code to be less generic than indicated by the type

How to change F# Interactive newline character

ε祈祈猫儿з 提交于 2019-12-05 22:36:14
In a .fs file a newline is denoted by \r\n , but in the F# Interactive window it is \n . In a problem I'm currently trying to solve, the length of a multiple line literal string matters. So a problem arises when I am testing code in the F# Interactive window, because the length of the string is different from in normal execution. I hope there is an option to change the newline 'character' in F# Interactive to \r\n , but I can't find it. Does anyone know where I can achieve this, or some other workaround? You can use conditional compilation to handle this: #if INTERACTIVE text.Replace("\n",

How to create a Union type in F# that is a value type?

十年热恋 提交于 2019-12-05 21:46:41
Normal F# Discriminated Unions are reference types. How can I create a simple (non-recursive and with only value-type fields) union type in F# that is a value type? Based on some internet searching my current (non-working) attempt looks as follows: [<StructLayout(LayoutKind.Explicit)>] type Float = [<DefaultValue>] [<FieldOffset 0>] val mutable Val1 : float [<DefaultValue>] [<FieldOffset 0>] val mutable Int1 : int new (a:float) = {Val1 = a} The following blog post appears to show what is possible via C# I'm aware that the above is NOT idiomatic use of F# but I am trying to optimize the

What is the current state of tail-call-optimization for F# on Mono (2.11)?

冷暖自知 提交于 2019-12-05 21:44:30
问题 What is the current state of Tail Call Optimization (TCO) implementation on Mono (2.11) ? Read somewhere that all the codebase would need to be modified to use a callee-pops-arguments convention. What is the status of this change ? Is the ARM/Linux port up to date on this matter ? Thanks! 回答1: Tail calls definitely work on mono on linux - tested using let rec f a = f (a+1) which didn't crash - tested on Mono 2.10.2 UPDATE Tested with link from Brian - https://bugzilla.novell.com/show_bug.cgi

Why doesn't F# support Edit and Continue? [closed]

我的梦境 提交于 2019-12-05 21:41:22
Visual Studio's Edit and Continue and other IDE's equivalent operations are very effective for experimentation in interactive applications, since you don't have to restart the app to see the effects of the code change (most of the time). If Visual Studio already supports this for C#, then why isn't it in F# yet? Does something about F# make it more difficult? It boils down to the fact that this kind of feature needs support in the compiler itself, and since the F# compiler has a much smaller team working on it than the C# compiler, they probably have other, higher priority items to work on. 来源