f#

F#: how to find Cartesian power

廉价感情. 提交于 2019-12-12 10:06:23
问题 I have a problem with writing a Cartesian power function. I found many examples about calculating Cartesian Product, but no one about Cartesian power. For example, [1;2] raised to power 3 = [ [1;1;1] ; [1;1;2] ; [1;2;1] ; [1;2;2] ; [2;1;1] ; [2;1;2] ; [2;2;1]; [2;2;2] ] I use following code to calculate Cartesian Product: let Cprod U V = let mutable res = [] for u in U do for v in V do res <- res @ [[u;v]] res And trying to calculate Cartesian power. I use following code to calculate

Discriminated union structural/custom equality

点点圈 提交于 2019-12-12 10:01:01
问题 I have the following discriminated union: type ActCard = Cellar of card list | Chapel of (card option * card option* card option* card option) | Smithy | Spy of (card -> bool * card -> bool) It had structural equality until I added the card -> bool to Spy . This question is helpful for how to do custom equality for records. However, I'm not sure how best to implement it in this situation. I would prefer to not have to enumerate each case in ActCard : override x.Equals(yobj) = match x, yobj

need help to read file with specific formatted contents

隐身守侯 提交于 2019-12-12 09:58:48
问题 i'm using F#. I want to solve some problem that require me to read the input from a file, i don't know what to do. The first line in the file consist of three numbers, the first two numbers is the x and y for an map for the next line. The example file: 5 5 10 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 the meaning of 5 5 10 is the next line have 5x5 map and 10 is just some numbers that i need to solve the problem, the next until the end of the line is contents of the map that i have to

How to change F# Interactive newline character

℡╲_俬逩灬. 提交于 2019-12-12 09:53:46
问题 In a .fs file a newline is denoted by \r\n , but in the F# Interactive window it is \n . In a problem I'm currently trying to solve, the length of a multiple line literal string matters. So a problem arises when I am testing code in the F# Interactive window, because the length of the string is different from in normal execution. I hope there is an option to change the newline 'character' in F# Interactive to \r\n , but I can't find it. Does anyone know where I can achieve this, or some other

Converting Haskell Polymorphic Cosine function to F#

时光总嘲笑我的痴心妄想 提交于 2019-12-12 09:44:49
问题 I'm trying to convert some Haskell code to F# but I'm having some trouble since Haskell is lazy by default and F# is not. I'm also still learning my way around F#. Below is a polymorphic cosine function in Haskell with pretty good performance. I want to try and keep the same or better performance parameters in F#. I would like to see a F# List version and a F# Seq version since the Seq version would be more like the lazy Haskell but the List version would probably perform better. Thanks for

Bottom/undefined value in F#?

半城伤御伤魂 提交于 2019-12-12 08:45:48
问题 There is a convenient undefined value in Haskell that can be used as a stub for yet to be defined functions/paths in code. Is there anything like it in F#? 回答1: To be concrete, you can define such a value like this: let undefined<'T> : 'T = failwith "Not implemented yet" let stub1 (x : int) : float = undefined let stub2 (x : 'T) : 'T = undefined Beware that F# evaluation is strict. If you bind undefined to a top-level value, it will throw an exception during evaluation. 回答2: I think failwith

Writing a polling Windows service

我是研究僧i 提交于 2019-12-12 08:38:35
问题 I usually write Windows services in C# but I'm giving it a go in F#. For a polling serivce, like this one, I ordinarily use a class I've written, which is similar to BackgroundWorker. It spawns a background thread and fires an OnWork method at regular intervals. (Complete code is here [github].) Is there another, perhaps better or more idiomatic, way to do this in F#? It could be a better way to write the polling background worker class, or built-in alternatives to it. EDIT Here's what I came

F# iteration over a Dictionary

北战南征 提交于 2019-12-12 08:35:08
问题 I'm just starting with F# and I want to iterate over a dictionary, getting the keys and values. So in C#, I'd put: IDictionary resultSet = test.GetResults; foreach (DictionaryEntry de in resultSet) { Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value); } I can't seem to find a way to do this in F# (not one that compiles anyway). Could anybody please suggest the equivalent code in F#? Cheers, Crush 回答1: What is the type of your dictionary? If it is non-generic IDictionary as your

Dynamic programming in F#

心不动则不痛 提交于 2019-12-12 08:25:31
问题 What is the most elegant way to implement dynamic programming algorithms that solve problems with overlapping subproblems? In imperative programming one would usually create an array indexed (at least in one dimension) by the size of the problem, and then the algorithm would start from the simplest problems and work towards more complicated once, using the results already computed. The simplest example I can think of is computing the Nth Fibonacci number: int Fibonacci(int N) { var F = new

Help Needed Creating a Binary Tree Given Truth Table

牧云@^-^@ 提交于 2019-12-12 08:10:48
问题 First, in order to provide full disclosure, I want to point out that this is related to homework in a Machine Learning class. This question is not the homework question and instead is something I need to figure out in order to complete the bigger problem of creating an ID3 Decision Tree Algorithm. I need to generate tree similar to the following when given a truth table let learnedTree = Node(0,"A0", Node(2,"A2", Leaf(0), Leaf(1)), Node(1,"A1", Node(2,"A2", Leaf(0), Leaf(1)), Leaf(0)))