f#

How do I write this member constraint in F#?

浪子不回头ぞ 提交于 2019-12-17 10:51:53
问题 For a type type Cow() = class member this.Walk () = Console.WriteLine("The cow walks.") end I can write a method which enforces a member constrain for method Walk like let inline walk_the_creature creature = (^a : (member Walk : unit -> unit) creature) // and then do walk_the_creature (Cow()) In this case the type is inferred. I am unable to explicitly write a constraint on the creature parameter like this // Does not compile // Lookup on object of indeterminate type based on information

While or Tail Recursion in F#, what to use when?

混江龙づ霸主 提交于 2019-12-17 10:38:14
问题 Ok, only just in F# and this is how I understand it now : Some problems are recursive in nature (building or reading out a treestructure to name just one) and then you use recursion. In these cases you preferably use tail-recursion to give the stack a break Some languagues are pure functional, so you have to use recursion in stead of while-loops, even if the problem is not recursive in nature So my question : since F# also support the imperative paradigm, would you use tail recursion in F#

While or Tail Recursion in F#, what to use when?

让人想犯罪 __ 提交于 2019-12-17 10:38:00
问题 Ok, only just in F# and this is how I understand it now : Some problems are recursive in nature (building or reading out a treestructure to name just one) and then you use recursion. In these cases you preferably use tail-recursion to give the stack a break Some languagues are pure functional, so you have to use recursion in stead of while-loops, even if the problem is not recursive in nature So my question : since F# also support the imperative paradigm, would you use tail recursion in F#

How to create a sequence of integers in C#?

匆匆过客 提交于 2019-12-17 10:33:20
问题 F# has sequences that allows to create sequences: seq { 0 .. 10 } Create sequence of numbers from 0 to 10. Is there something similar in C#? 回答1: You can use Enumerable.Range(0, 10); . Example: var seq = Enumerable.Range(0, 10); MSDN page here. 回答2: Enumerable.Range(0, 11); Generates a sequence of integral numbers within a specified range. http://msdn.microsoft.com/en-us/library/system.linq.enumerable.range.aspx 回答3: You could create a simple function. This would work for a more complicated

Can you mix .net languages within a single project?

喜你入骨 提交于 2019-12-17 09:30:13
问题 Can you mix .net languages within a single project? So pre-compiled, I would like to call classes and methods of other source files. For both web and apps? In particular I'd be interested in F# and C#. 回答1: You can mix languages in a single assembly with ILMerge and MSBuild. Here is a very good example about it. 回答2: Yes, you can, but Visual Studio does not support it directly. What you will do is compile code to netmodules, then combine them into a single assembly. The compilers support the

F# generics / function overloading syntax

非 Y 不嫁゛ 提交于 2019-12-17 07:33:27
问题 I'm confused on how to label a function as generic without an explicit type declaration like ('a -> 'a) let add a b = a + b This gives us val add : a:int -> b:int -> int However we can then immediately call add "Hello " "World!" and now the value of add is val add : a:string -> b:string -> string val it : string = "Hello World!" If we then call add 2 3 // then we get error: This expression was expected to have type string but here has type int How do I ensure that a function works on all

How to Async.AwaitTask on plain Task (not Task<T>)?

允我心安 提交于 2019-12-17 06:35:37
问题 I'm trying to consume a C# library in F#. The library makes heavy use of async/await. I want to use within an async { ... } workflow in F#. I see we can Async.AwaitTask on async C# methods returning Task<T> , but what about those returning plain Task ? Perhaps, is there a helper to convert these to Async<unit> or to convert Task to Task<unit> so it will work with Async.AwaitTask ? 回答1: You can use ContinueWith: let awaitTask (t: Task) = t.ContinueWith (fun t -> ()) |> Async.AwaitTask Or

C# / F# Performance comparison

£可爱£侵袭症+ 提交于 2019-12-17 06:28:15
问题 Is there any C#/F# performance comparison available on web to show proper usage of new F# language? 回答1: Natural F# code (e.g. functional/immutable) is slower than natural (imperative/mutable object-oriented) C# code. However, this kind of F# is much shorter than usual C# code. Obviously, there is a trade-off. On the other hand, you can, in most cases, achieve performance of F# code equal to performance of C# code. This will usually require coding in imperative or mutable object-oriented

Generate tail call opcode

99封情书 提交于 2019-12-17 06:12:40
问题 Out of curiosity I was trying to generate a tail call opcode using C#. Fibinacci is an easy one, so my c# example looks like this: private static void Main(string[] args) { Console.WriteLine(Fib(int.MaxValue, 0)); } public static int Fib(int i, int acc) { if (i == 0) { return acc; } return Fib(i - 1, acc + i); } If I build it in release and run it without debugging I do not get a stack overflow. Debugging or running it without optimizations and I do get a stack overflow, implying that tail

Generate tail call opcode

爱⌒轻易说出口 提交于 2019-12-17 06:12:09
问题 Out of curiosity I was trying to generate a tail call opcode using C#. Fibinacci is an easy one, so my c# example looks like this: private static void Main(string[] args) { Console.WriteLine(Fib(int.MaxValue, 0)); } public static int Fib(int i, int acc) { if (i == 0) { return acc; } return Fib(i - 1, acc + i); } If I build it in release and run it without debugging I do not get a stack overflow. Debugging or running it without optimizations and I do get a stack overflow, implying that tail