f#

Combinations and Permutations in F#

懵懂的女人 提交于 2019-12-17 22:35:03
问题 I've recently written the following combinations and permutations functions for an F# project, but I'm quite aware they're far from optimised. /// Rotates a list by one place forward. let rotate lst = List.tail lst @ [List.head lst] /// Gets all rotations of a list. let getRotations lst = let rec getAll lst i = if i = 0 then [] else lst :: (getAll (rotate lst) (i - 1)) getAll lst (List.length lst) /// Gets all permutations (without repetition) of specified length from a list. let rec getPerms

F# operator “?”

别来无恙 提交于 2019-12-17 22:17:46
问题 I just read the information on this page, and while a new ? operator is mentioned, it's quite unclear to me what would its usage be. Could anyone please provide a quick explanation, post a code snipped of how would this operator be used and possibly mention a use case? Edit : this is really awkward, I've noticed that the ? operator is no longer mentioned in Don's release notes. Any idea of why is that? 回答1: There are two new "special" operators in this F# release, (?) and (?<-). They are not

Can ML functors be fully encoded in .NET (C#/F#)?

心不动则不痛 提交于 2019-12-17 21:56:50
问题 Can ML functors be practically expressed with .NET interfaces and generics? Is there an advanced ML functor use example that defies such encodings? Answers summary : In the general case, the answer is NO. ML modules provide features (such as specification sharing via signatures [1]) that do not directly map to .NET concepts. However, for certain use cases the ML idioms can be translated. These cases include not only the basic Set functor [2], but also the functorial encoding of monads [3],

Object-oriented programming in a purely functional programming context?

╄→尐↘猪︶ㄣ 提交于 2019-12-17 21:43:04
问题 Are there any advantages to using object-oriented programming (OOP) in a functional programming (FP) context? I have been using F# for some time now, and I noticed that the more my functions are stateless, the less I need to have them as methods of objects. In particular, there are advantages to relying on type inference to have them usable in as wide a number of situations as possible. This does not preclude the need for namespaces of some form, which is orthogonal to being OOP. Nor is the

F#: Not understanding match .. with

我只是一个虾纸丫 提交于 2019-12-17 21:32:13
问题 I'm messing around with F# and Fable, and trying to test my understanding. To do so, I tried creating a function to calculate e given a certain number of iterations. What I've come up with is let eCalc n = let rec internalECalc ifact sum count = match count = n with | true -> sum | _ -> internalECalc (ifact / (float count)) (sum + ifact) (count+1) internalECalc 1.0 0.0 1 Which works fine, returning 2.7182818284590455 when called with eCalc 20 However, if I try using, what I think is, the more

Best approach for designing F# libraries for use from both F# and C#

∥☆過路亽.° 提交于 2019-12-17 21:24:01
问题 I am trying to design a library in F#. The library should be friendly for use from both F# and C# . And this is where I'm stuck a little bit. I can make it F# friendly, or I can make it C# friendly, but the problem is how to make it friendly for both. Here is an example. Imagine I have the following function in F#: let compose (f: 'T -> 'TResult) (a : 'TResult -> unit) = f >> a This is perfectly usable from F#: let useComposeInFsharp() = let composite = compose (fun item -> item.ToString)

Are there tricks for doing implicit conversions in F#?

不打扰是莪最后的温柔 提交于 2019-12-17 21:05:58
问题 Consider this F# code to sum the numbers below i that are multiples of 3 and 5: let isMultipleOfThreeOrFive n = (n % 3 = 0) || (n % 5 = 0) let sequenceOfMultiples i = seq {1 .. i - 1} |> Seq.filter isMultipleOfThreeOrFive Since i is an int , you'll overflow if i is large. This version with BigInteger takes care of that: let isMultipleOfThreeOrFive n = (n % 3I = 0I) || (n % 5I = 0I) let sequenceOfMultiples (i : System.Numerics.BigInteger) = seq {1I .. i - 1I} |> Seq.filter

OCaml performance of exceptions

放肆的年华 提交于 2019-12-17 20:44:43
问题 I've often read that exceptions are somewhat slow and should be avoided if performance is an issue (for instance, in Java, F#, etc). Does that apply to common OCaml functions such as Hashtbl.find , which return exceptions for elements not found? In particular, if I want my application to be efficient, should I always test element membership using, for instance, Hashtable.mem before calling Hashtbl.find ? Or would the extra comparison of the mem function negatively impact performance? 回答1:

Assignment operator in f#

余生长醉 提交于 2019-12-17 20:39:06
问题 i have seen in ruby as well powershell programming we can assign variables like a,b=b,a . it actually swaps the variable . Is this possible in f# if so please guide me with some reference 回答1: Generally, F# doesn't allow variable re-assignment. Rather it favors immutable named values via let bindings. So, the following is not possible: let a = 3 a = 4 Unless you explicitly mark a as mutable : let mutable a = 3 a <- 4 However, F# does allow in most situations variable "shadowing". The only

Using c# delegates with f# functions

北城以北 提交于 2019-12-17 20:21:29
问题 I am trying to call a c# function from f# where the c# function takes a function (delegate?) as a parameter and I need this argument to be a f# function. Eg: Sample c# public static void function_1(double x, ref double y) { y = Math.Exp(x); } main() { state s; call_func(s, function_1) } So, call_func has a parameter of type void fn(double, ref double) In f# I tried: let function_1 (x:double) (y:double byref) = let y = 6.0 () let test = let s = new state let ret = call_func(s, function_1) But