f# - Explaining pattern matching vs switch

I have been trying to explain the difference between switch statements and pattern matching(F#) to a couple of people but I haven't really been able to explain it well..most of the time they just look at me and say "so why don't you just use if..then..else".How would you explain it to them? EDIT! Thanks everyone for the great answers, I really wish I could mark multiple right answers....Read more

functional programming - F# combining two sequences

I have two sequences that I would like to combine somehow as I need the result of the second one printed right next to the first. The code is currently where playerItems refers to a list:seq state.player.playerItems |> Seq.map (fun i -> i.name) |> Seq.iter (printfn "You have a %s")seq state.player.playerItems |> Seq.map (fun i -> i.description) |> Seq.iter (printfn "Description = %s")The result currently isYou have a KeycardYou have a HammerYou have a WrenchYou have a ScrewDescription = Swipe to enterDescripti...Read more

f# - Why is $ allowed but $$, or <$> disallowed as an operator (FS0035) and what makes $ special?

$ is allowed in a custom operator, but if you try to use $$, <$> or for instance ~$% as operator name you will receive the following error: error FS0035: This construct is deprecated: '$' is not permitted as a character in operator names and is reserved for future use$ clearly also has the '$' in the name, but works, why?I.e.:let inline ( $ ) f y = f y// using it works just fine:let test = let add x = x + 1 add $ 12I see $ a lot in online examples and apparently as a particular kind of operator. What is this spcial treatment or ro...Read more

Can I create a StackLayout with F# in Xamarin Forms?

In his Xamarin 3 F# Awesomeness blog posting, Dave Thomas shows a StackLayout being created:StackLayout.Create( [ Entry (Placeholder = "Username") Entry (Placeholder = "Password", IsPassword = true) Button (Text = "Login", TextColor = Color.White, BackgroundColor = Color.FromHex "77D065") ],...However, I get an error The field, constructor or member 'Create' is not definedIf I take out the .Create before the parens, the error message changes to The member of object constructor 'St...Read more

f# - How to close a stream properly?

I have been assigned a task to write a program that will:Open a file.Read the content.Replace a specific word with another word.Save the changes to the file.I know for sure that my code can open, read and replace words. The problem occurs when i add the "Save the changes to the file" - part. Here is the code:open System.IO//Getting the filename, needle and replace-word.System.Console.WriteLine "What is the name of the file?"let filename = string (System.Console.ReadLine ())System.Console.WriteLine "What is your needle?"let needle = string (Syst...Read more

visual studio 2017 - Loading F# modules not in the current directory

I'm writing a simple F# console app with Visual Studio 2017 targeting .NET Core 2.0. I'm wondering if it's possible to open a module not present in the current directory.For example :/root/foo.fs/root/SubDirectory/bar.fsHow can bar.fs open the Foo module or vice versa?I already tried : open Fooopen "../Foo"open "C:/AbsolutePath/Foo"With the last two ones, VS complained with : FS0010 : Unexpected string literal in open declaration. Expected identifier, 'global' or other token.There seems to be no documentation about this.Any help would be appr...Read more

functional programming - Is it possible that F# will be optimized more than other .Net languages in the future?

Is it possible that Microsoft will be able to make F# programs, either at VM execution time, or more likely at compile time, detect that a program was built with a functional language and automatically parallelize it better? Right now I believe there is no such effort to try and execute a program that was built as single threaded program as a multi threaded program automatically. That is to say, the developer would code a single threaded program. And the compiler would spit out a compiled program that is multi-threaded complete with mutexes a...Read more

f# - N-gram split function for string similarity comparison

As part of excersise to better understand F# which I am currently learning , I wrote function to split given string into n-grams. 1) I would like to receive feedback about my function : can this be written simpler or in more efficient way? 2) My overall goal is to write function that returns string similarity (on 0.0 .. 1.0 scale) based on n-gram similarity; Does this approach works well for short strings comparisons , or can this method reliably be used to compare large strings (like articles for example). 3) I am aware of the fact that n-gram...Read more

f# - FSharp.Core FSharpFunc.FromConverter in netstandard20

In converting a project to .NET Standard 2.0, FSharpFunc<T, TResult>. FromConverter method from the FSharp.Core library is not available anymore. Is there a way to convert this code to a .NET Standard 2.0 FSharp.Core implementation?async Task<Unit> RunProcess(FSharpMailboxProcessor<T> mailbox, Func<T, Task> process) { ... }public BaseMailboxProcessor(Func<TFuncInput, Task> process, Action<Exception, TFuncInput> errorHandler = null, CancellationToken? cancellationToken = null){ m_ErrorHandler = errorHandler...Read more

xamarin.forms - Is it currently impossible to publish UWP apps to Store when using F#?

I have a Xamarin.Forms app on Android, iOS, and UWP that's already published to all relevant app stores. It's written in C#, but a new feature was recently implemented partly in F#. This means I can't use .NET Native compilation on my UWP project. I turned off .NET Native compilation, but discovered that I now can't publish updates to Windows Store any longer.Is there any way around this, or is it simply impossible for the time being to publish Xamarin.Forms apps using F# to the Windows Store? I know there's something called Desktop bridge, but...Read more

f# - IObservable subscribe and publish in Windows Service

Hi I am trying to implement a windows service that subscribes and publishes to IObservables.I have used http://blogs.tedneward.com/2010/11/20/Windows+Service+In+F.aspx from Ted Neward's blog as a starting point. type WindowsService() as this let createAndPublishObservables() = let obs1 = createObservable1() let obs2 = createObservable2() let combObs = Observable.CombineLatest(obs1, obs2) |> map ( fun (firstObsValue, secObsValue) -> firstObsValue + secObsValue) let obsUpdater = new ObsUpdater() let updaterPublish = combOb...Read more

f# - References don't show up in Visual Studio 2017 after creating a .NET Standard branch

I'm working with Visual Studio 2017 on the open-source project https://github.com/SpiegelSoft/XamarinForms.Reactive.FSharpI recently created a branch called dotnet-standard, which, as its name suggests, is a migration to .NET Standard. However, after creating this branch and checking back in to the master branch, which is a PCL rather than a .NET standard library, the project will no longer build.First of all, the references are not visible. All I can see in Visual Studio is a .NET node (see screenshot).The build fails in the first instance wit...Read more

F# - Weight (Unit of Measure) Stones-Pounds

I have a csv file containing daily weights as follows:Date,Name,Weight11-Sep-2017,Alpha,9-113-Sep-2017,Alpha,8-1315-Sep-2017,Alpha,8-11Though I can successfully import them using CsvProvider, the weight column defaults to System.DateTime. // Weight [<Measure>] type lb [<Literal>] let input = "DayWeights.csv" type Weights = CsvProvider<input, HasHeaders=true> let data = Weights.GetSample() for row in data.Rows do printfn "Output: (%A, %A, %A)" row.Date row.Name row.WeightIs it possible to create a Un...Read more

interop - How can I create a measured record type without a measure in F#

I have defined a record type with a unit of measure in a common library (LibRoot) that is used by both C# (LibC) and F# (LibF) code. I then wrote a public API in the C# library (LibC) that the F# library (LibF) consumes. But any time i attempt to pass an object to this API F# complains that it must have a unit of measure.//LibRoot - F#type Vec2<[Measure] 'u> = { X : int Y : int}//LibC - C#public static class Funcs{ public void DoWork(Vec2 vec) { //no measure needed in C# .... }}//LibF - F#open LibClet myVec : Vec2<1&...Read more