f#

Creating Set in F# with elements from 1111 to 6666

风格不统一 提交于 2019-12-25 18:54:27
问题 How do i create a Set in F# with elements from 1111 to 6666 without any values being 0, 7 or higher. E.g. [1111,1112,1113,1114,1115,1116,1121] I'd like to make it a set. Thanks in advance 回答1: You can use a sequence comprehension: let values = seq { for i in 1110 .. 10 .. 6660 do for j in 1 .. 6 do yield i + j } and create a set using Set.ofSeq e.g. let s = Set.ofSeq values 回答2: There must be an easier way than: let values = seq { for a in 1000 .. 1000 .. 6000 do for b in 100 .. 100 .. 600 do

Creating Set in F# with elements from 1111 to 6666

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 18:54:08
问题 How do i create a Set in F# with elements from 1111 to 6666 without any values being 0, 7 or higher. E.g. [1111,1112,1113,1114,1115,1116,1121] I'd like to make it a set. Thanks in advance 回答1: You can use a sequence comprehension: let values = seq { for i in 1110 .. 10 .. 6660 do for j in 1 .. 6 do yield i + j } and create a set using Set.ofSeq e.g. let s = Set.ofSeq values 回答2: There must be an easier way than: let values = seq { for a in 1000 .. 1000 .. 6000 do for b in 100 .. 100 .. 600 do

F# Threading Changing State Through Unfold

风流意气都作罢 提交于 2019-12-25 16:27:37
问题 I'm trying to process a sequence of items whereby the process step relies on some additional cumulative state from the prior items (ordering isn't important). Essentially: I have a Seq<'A> I have a (Type * int) list referred to as the skip list I have a process step 'A -> (Type * int) list -> 'B option This takes the current skip list The method in question essentially: Seq<'A'> -> (Type * int) list -> (Type * int) list So we take a bunch of input items and an initial skip list and produce a

Autocomplete in F# 2008

≡放荡痞女 提交于 2019-12-25 12:41:41
问题 Is there a way to get autocomplete in F# with Visual Studio 2008? That is, true auto completion (suggest words as soon as you start typing), not the kind where you have to manually press a key combination? Or, if not, since it does work in Visual C# 2010: is the lack of it because it's something C# has and F# doesn't, or because it's something Visual Studio 2010 has and 2008 doesn't? 回答1: It's a C#/F# difference. The F# autocompletion does not start automatically until after you are 'dotting

F# type mismatch while calling function

此生再无相见时 提交于 2019-12-25 11:57:12
问题 This code let rec readNLines n list = if n = 0 then list else readNLines(n-1,readInt()::list) ends with Type mismatch. Expecting a 'a but given a 'a -> 'a The resulting type would be infinite when unifying ''a' and ''a -> 'a' (using built-in F# compiler) but runs ok when last line is changed to readNLines(n-1,(readInt()::list)) or readNLines(n-1)(readInt()::list) Question is: Why? :| 回答1: Only the last version can work, because readNLines takes two arguments, but readNLines (n - 1, readInt()

functions in F# .. why is it not compiling

倖福魔咒の 提交于 2019-12-25 08:30:04
问题 I have written two versions of code. The first one works as expected and print "Hi". the second one gives me error that "block following this let is unfinished" 1st version #light let samplefn() = let z = 2 let z = z * 2 printfn "hi" samplefn() 2nd version #light let samplefn() = let z = 2 let z = z * 2 samplefn() Only difference is the printfn is absent in the second version. I am using Visual Studio 2010 as my IDE. I am very new to F# but this error seems very strange to me. I guess I am

Built-in “< > compare” doesn't work with “IComparable<T>”?

ε祈祈猫儿з 提交于 2019-12-25 05:18:26
问题 I have a Discriminated Union, and I hope to use built in operators like > < compare max for it. [<CustomComparison>] type SymbolType = | A | B | C | D interface IComparable<SymbolType> with member x.CompareTo y = match x, y with | A, A-> 0 | A, _ -> 1 | _, A-> -1 | _, _ -> 0 I understand I can use IComparable , but then i have to do a null check, what's worse is that I have to cast it like (SymbolType) y which I assume would be time consuming. 回答1: You can just implement the required methods

Self-error-dependent self-disposal of IObservable subscriptions

霸气de小男生 提交于 2019-12-25 05:04:26
问题 I have F# code that looks like this: module O = Control.Observable //... use ss = serve' 4000 |> O.subscribe (fun c -> use cs = RSS.items |> O.subscribe (bytes >> c.SendAll) |> ignore) where serve' : int -> IObservable<Socket> c : Socket RSS.items : IObservable<XElement> bytes : XElement -> byte [] c.SendAll : byte [] -> unit What is the most idiomatic way to retain cs until c.SendAll fails? Is there or is it possible to define Observable.subscribeUntilError(action) where if action fails,

F# Extension Methods on Lists, IEnumerable, etc

雨燕双飞 提交于 2019-12-25 04:45:32
问题 In C#, if I had a widget definition, say: class widget { public string PrettyName() { ... do stuff here } } and I wanted to allow for easy printing of a list of Widgets, I might do this: namespace ExtensionMethods { public static PrintAll( this IEnumerable<Widget> widgets, TextWriter writer ) { foreach(var w in widgets) { writer.WriteLine( w.PrettyName() ) } } } How would I accomplish something similar with a record type and a collection (List or Seq preferrably in F#). I'd love to have a

F#: Reduce a list of tuples by grouping one of the elements into arrays

与世无争的帅哥 提交于 2019-12-25 02:29:06
问题 I have a list of tuples which i want to group by one of its elements as a key. For example, if i had this list of tuples: [(A, "hello"), (A, "stack"), (A,"over"), (A, "flow"), (B, "how"), (B, "you"), (C, "doin")] I would like to get a result in the form: [(A, ["hello", "stack", "over", "flow"]), (B, ["how", "you"]), (C, ["doin"])] I am new to F# so I am all out of ideas on how to do this. I thank you in advance. cheers 回答1: I think you are using incorrect delimiter for list elements - instead