f#

F# compiler error “This expression was expected to have type unit but here has type bool.” expression in {if else} statements

不打扰是莪最后的温柔 提交于 2019-12-10 17:14:29
问题 I have written such a function in F#: let TwistBasket (reverse: bool, quarters: int, overTwist: int byref) = overTwist <- 50 WaitForBasketReady() waitBasket.Reset() let move = 135*quarters - 25 + overTwist let speed = match reverse with | true -> -75y | false -> 75y let waitHandle = motorBasket.SpeedProfile(speed, 15u, uint32 move, 10u, true) Task.Factory.StartNew(fun () -> waitHandle.WaitOne() if (overTwist <> 0) then motorBasket.SpeedProfile(sbyte -speed, 0u, uint32 overTwist, 0u, true)

Can a function be optimized for tail recursion even when there are more than one distinct recursive calls?

拈花ヽ惹草 提交于 2019-12-10 17:13:16
问题 As I mentioned in a recent SO question, I'm learning F# by going through the Project Euler problems. I now have a functioning answer to Problem 3 that looks like this: let rec findLargestPrimeFactor p n = if n = 1L then p else if n % p = 0L then findLargestPrimeFactor p (n/p) else findLargestPrimeFactor (p + 2L) n let result = findLargestPrimeFactor 3L 600851475143L However, since there are 2 execution paths that can lead to a different call to findLargestPrimeFactor , I'm not sure it can be

F# basics: turning NameValueCollection into nice string

∥☆過路亽.° 提交于 2019-12-10 17:12:39
问题 Learning F# while trying to do something useful at the same time, so this is kind of basic question: I have req , which is a HttpListenerRequest , which has QueryString property, with type System.Collections.Specialized.NameValueCollection . So, for sake of clarity let's say, I have let queryString = req.QueryString Now I want to produce nice string (not printf to console) from contents of that, but queryString.ToString() is not overriden apparently, so it just gives string "System

How to generalize f# option?

我们两清 提交于 2019-12-10 17:09:43
问题 I'm getting result of some calculations in form of 'a option when 'a :> IBaseType . There is tree of types derived from IBaseType , and I don't really know option of what particular type this one is, but the important thing is that it's option of specific derived, not base type. So I want to upcast it to IBaseType option to process it further. As option is generic type, it's impossible to do cast directly (in F#), and I have to do cast inside of Option.map. Nothing complicated, type inference

Calculating differences of subsequent elements of a sequence in F#

ぃ、小莉子 提交于 2019-12-10 17:09:12
问题 I have a sequence of floats in F#, and I need to get a sequence defined of (Math.Log currentElement)/(Math.Log previousElement) . Obviously it will be shorter than the original sequence by one element. What is the most elegant way to achieve this in F#? I was thinking to use a seq{} expression with a for loop inside, but even then handling the first element in a reasonably nice way seems difficult... 回答1: items |> Seq.pairwise |> Seq.map (fun (x, y) -> log y / log x) 回答2: Or if you prefer:

What is the equivalent of Scala's Seq.Span in F#?

只愿长相守 提交于 2019-12-10 17:06:33
问题 To quote Scala's documentation: def span(p: (A) => Boolean): (Seq[A], Seq[A]) Splits this iterable collection into a prefix/suffix pair according to a predicate. Note: c span p is equivalent to (but possibly more efficient than) (c takeWhile p, c dropWhile p), provided the evaluation of the predicate p does not cause any side-effects. Note: might return different results for different runs, unless the underlying collection type is ordered. p - the test predicate returns - a pair consisting of

F#: Can units of measure be bound dynamically at runtime?

孤者浪人 提交于 2019-12-10 16:59:04
问题 I'm very new to F# and am intrigued by the Units of Measure functionality and have a rough idea of how it works normally, but would like to know if it's possible to bind measures to values where we don't know what the measure will be until the code is executing? The practical example I'm looking at is binding floats as currency values where the unit of measure is inferred from a database lookup. Let's assume that the measures for each currency (USD, EUR, AUD, etc) are declared normally: [

F# mutable list is null

梦想与她 提交于 2019-12-10 16:55:47
问题 When I try to run the code below, properties is null. Why is that? I assign an empty list to properties, but the first time through the loop, it is null. This causes it to drop the first value that I append to it. I do not understand this either. It seems that a value concatenated with a null should be list of the value. [<TechTalk.SpecFlow.Binding>] module FSharpLeftistHeapStepDefinition open TechTalk.SpecFlow let mutable properties = [0] let [<Given>] ``I have a list with the following

Is there a pre-existing function to pass a tuple as args?

瘦欲@ 提交于 2019-12-10 16:45:41
问题 I want to apply a regular function that takes two arguments to a tuple. I defined a helper function like so: let apply f (a, b) = f a b However I feel like this must be a fairly common use case and wondered if it was a function in the standard library (I'm not sure what terms to search for) 回答1: The double forward pipe (||>) does this. 来源: https://stackoverflow.com/questions/21840206/is-there-a-pre-existing-function-to-pass-a-tuple-as-args

F# JSON parsing - How to get property using complex path (consisting of several propery names)

风流意气都作罢 提交于 2019-12-10 16:38:37
问题 Is it possible to get property from JSON using complex path, like "prop1.prop2" ? Sample of JSON I used: { "prop1": { "prop2": "value" } } What I have want is to get property "prop2" there with its value "value" : When I tried: #r "../packages/FSharp.Data.2.3.0/lib/net40/FSharp.Data.dll" open FSharp.Data open FSharp.Data.JsonExtensions let json = JsonValue.Load "SampleJson.json" json.GetProperty("prop1.prop2") I got: System.Exception: Didn't find property 'prop1.prop2' in {"prop1":{"prop2":