f#

What are the primary differences between Haskell and F#? [closed]

这一生的挚爱 提交于 2020-07-14 12:51:20
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Closed 6 years ago . Locked . This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions. I've searched on the Internet for comparisons between F# and Haskell but haven't found anything really definitive. What are the primary differences and why would I want to choose one over the other? 回答1: Haskell

Can't understand the logic of F# mutable variable inside function body

两盒软妹~` 提交于 2020-07-09 12:43:19
问题 I'm learning F# and get stuck with the concept of mutable keyword. Please see the below example: let count = let mutable a = 1 fun () -> a <- a + 1; a val count: unit -> int Which increases by 1 every time it's called with () . But next code does not: let count = let mutable a = 1 a <- a + 1 a val count: int Which is always 2 . In the book I'm studying with, it says with the first example, "The initialization of mutable value a is done only once, when the function has called first time." When

Mismatch in record pattern and type on database record in F#

試著忘記壹切 提交于 2020-07-09 12:04:28
问题 How do I fix the following errors? This expression was expected to have type 'string * Nullable * Nullable' but here has type 'VisitType' (Error occurs on "|AppointmentOnly(q.lastname, q.posting_time)). and Field 'appointmentTime' is not static (Error occurs on FsVisit.appointmentTime = q.appointment_time;). (These errors occur in my attempt to download records from a PostgreSQL database via WCF into F# client). type VisitType = | AppointmentOnly of name: string * postedTime: DateTime |

Using Foq with F# function types

回眸只為那壹抹淺笑 提交于 2020-07-08 11:52:48
问题 I am using F# type definitions to prevent hard dependencies between my functions, for example type IType1 = int -> int type IType2 = int-> string let func1 (i : int) : int = i * i let func2 (i : int) : string = i |> string let higherFunc (dep1 : IType1) (dep2 : IType2) (input : int) : string = input |> dep1 |> dep2 let curriedFunc = higherFunc func1 func2 let x = curriedFunc 2 output x : "4" Obviously this is quite contrived and simple but imagine the dependencies are a parser and a sorter or

IgnoreMissingMember setting doesn't seem to work with FSharpLu.Json deserializer

两盒软妹~` 提交于 2020-07-03 09:59:11
问题 This is a following to: deserialization issue, with json.net, in F#. I am deserializing some JSON that has an extra, unbound property using FSharpLu.Json. Here is the code: open System open Newtonsoft.Json open Microsoft.FSharpLu.Json type r = { a: int } let a = "{\"a\":3, \"b\":5}" Compact.TupleAsArraySettings.settings.MissingMemberHandling <- MissingMemberHandling.Ignore Compact.deserialize<r> a // doesn't work Despite setting MissingMemberHandling.Ignore it returns a json.net error: Could

Another failure at deserializing data with discriminated unions, in F#

我怕爱的太早我们不能终老 提交于 2020-06-29 03:47:31
问题 Following a question where the answer provided a working solution to serialize / deserialize discriminated unions (IgnoreMissingMember setting doesn't seem to work with FSharpLu.Json deserializer) I have now a practical case where this fails (although it works in simpler cases). here is the test code: open System.Collections.Generic open Microsoft.FSharpLu.Json open Newtonsoft.Json open Newtonsoft.Json.Serialization // set up the serialization / deserialization based on answer from: // https:

Wrapping a recursive function to count the number of function calls

假装没事ソ 提交于 2020-06-28 10:11:52
问题 Say I have a recursive function that I want to know how many times the function has called itself per input value. Rather than putting printf expressions or changing the return type to include the number of calls, is it possible to "wrap" the function with another to achive this? I would like the wrapped function to return the number of function calls and the original functions result. It should be reusable across different functions. Here is what I have and it doesn't work. open System open

Wrapping a recursive function to count the number of function calls

徘徊边缘 提交于 2020-06-28 10:11:27
问题 Say I have a recursive function that I want to know how many times the function has called itself per input value. Rather than putting printf expressions or changing the return type to include the number of calls, is it possible to "wrap" the function with another to achive this? I would like the wrapped function to return the number of function calls and the original functions result. It should be reusable across different functions. Here is what I have and it doesn't work. open System open

What's the difference between Lazy.Force() and Lazy.Value

不想你离开。 提交于 2020-06-27 07:22:18
问题 On the MSDN documentation for Lazy.Force<T> extension method says: Forces the execution of this value and returns its result. Same as Value. Mutual exclusion is used to prevent other threads from also computing the value. Does it mean that it's equivalent to creating a Lazy<T> instance with ExecutionAndPublication LazyThreadSafetyMode so that only one thread can initialize the instance? Thanks 回答1: Yes. They are both the same, and both make sure that the value will be computed only once. 回答2:

F# type constraints and reflection

旧街凉风 提交于 2020-06-25 03:33:29
问题 Is there any way to determine whether a given type parameter satisfies the F# comparison constraint through reflection? I would suspect not, since the expression typedefof<Set<_>>.MakeGenericType [| typeof<System.Type> |] appears to yield no errors. Still, I would like to hear some authoritative opinion on this. 回答1: Quoting from Don Syme's thorough post on equality and comparison constraints: The constraint type : comparison holds if: if the type is a named type, then the type definition