f#

Why does function containing Console.ReadLine() not complete?

跟風遠走 提交于 2019-12-23 10:25:51
问题 I am using Visual Studio 2012, and the function that calls Console.ReadLine() will not execute let inSeq = readlines () in this simple program open System open System.Collections.Generic open System.Text open System.IO #nowarn "40" let rec readlines () = seq { let line = Console.ReadLine() if not (line.Equals("")) then yield line yield! readlines () } [<EntryPoint>] let main argv = let inSeq = readlines () 0 I've been experimenting and researching this, and cannot see what is probably a very

fscheck generating string with size between min & max

南楼画角 提交于 2019-12-23 10:18:38
问题 I try to write a FsCheck generator that generates strings with length in a given interval. My attempt is the following: let genString minLength maxLength = let isValidLength (s : string) = s.Length >= minLength && s.Length <= maxLength Arb.generate |> Gen.suchThat isValidLength |> Arb.fromGen ...and I get the error: "System.Exception : No instances of class FsCheck.Arbitrary`1[a] for type System.String with arguments set []" What am I doing wrong? thanks! UPDATE 1: I managed to write the

Whats wrong with s.Count(Char.IsLetter)

随声附和 提交于 2019-12-23 10:18:00
问题 F# let s = "bugs 42 bunny" s.Count(fun c -> Char.IsLetter(c)) s.Where(fun c -> Char.IsLetter(c)).ToArray() s.Where(Char.IsLetter).ToArray() s.Count(Char.IsLetter) // error Why does only the last line fail to compile: Error FS0002: This function takes too many arguments, or is used in a context where a function is not expected 回答1: I think it's an edge case of type inference wrt member overloading. The difference between Count and Where is that the former has two overloads with different

F# Split Function

两盒软妹~` 提交于 2019-12-23 10:14:24
问题 I'm building a merge sort function and my split method is giving me a value restriction error. I'm using 2 accumulating parameters, the 2 lists resulting from the split, that I package into a tuple in the end for the return. However I'm getting a value restriction error and I can't figure out what the problem is. Does anyone have any ideas? let split lst = let a = [] let b = [] let ctr = 0 let rec helper (lst,l1,l2,ctr) = match lst with | [] -> [] | x::xs -> if ctr%2 = 0 then helper(xs, x::l1

Einstein's Riddle solution using F# [closed]

烂漫一生 提交于 2019-12-23 10:04:44
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . I'm looking for Einstein's Riddle solution using F# and I've found only Einstein meets F#. Is F# suitable for this problem and are there any other implementations? 来源: https://stackoverflow.com/questions/10341464

Generic zero for generic function

旧城冷巷雨未停 提交于 2019-12-23 09:59:53
问题 I have a function to calculate the cumulated sum of a sequence. let cumsum<'T> = Seq.scan (+) 0 >> Seq.skip 1 >> Seq.toArray Though it looks generic, the integer 0 makes it non-generic, and thus I cannot call the function with a sequence of floats. Is there a generic zero that can replace my hardcoded 0 , or maybe a different way of making the function generic. 回答1: You can use the GenericZero primitive but you need to make your function inline and make it explicitly a function (right now

Static constructors in F# - when do they run?

和自甴很熟 提交于 2019-12-23 09:54:48
问题 I am experimenting with various ways of creating singletons in F#, so that I understand the subtleties better. I don't know if the singleton pattern is ever useful in F#, but I wanted to experiment. And I was surprised by one result involving static constructors on those singleton instances. First I'll show you my code, and then I'll go into more details about my question. In one project called TrySingleton , I created three modules. Here's Eager.fs : module TrySingleton.Eager type EagerClass

Intersection between two lists F#

五迷三道 提交于 2019-12-23 09:49:04
问题 I am looking for a function that takes the intersection between two lists and creates a new list, I have this function: let intersect x y = Set.intersect (Set.ofList x) (Set.ofList y) that do what I ant but I dont want to use any of the inbuilt functions in F# 回答1: It is best to use the library stuff, but if you can't If we assume that the input lists are sorted (use List.sort or write your own) : let rec intersect a b = match a with |h::t -> match b with |h2::t2 -> if h=h2 then h::(intersect

F# interactive, API restriction on dll referencing

别说谁变了你拦得住时间么 提交于 2019-12-23 09:32:04
问题 How do you solve the error message that looks like this? `Binding session to 'C:\Program Files (x86)\NLog\.NET Framework 4.0\NLog.dll'... error FS0193: API restriction: The assembly 'file:///C:\Program Files (x86)\NLog\.NET Framework 4.0\NLog.dll' has already loaded from a different location. It cannot be loaded from a new location within the same appdomain. Code that triggers it, might look like this: #r @"..\packages\NLog.2.0.0.2000\lib\net20\NLog.dll" NLog.Config.SimpleConfigurator

Type annotation for using a F# TypeProvider type e.g. FSharp.Data.JsonProvider<…>.DomainTypes.Url

不羁的心 提交于 2019-12-23 09:30:00
问题 I'm using the FSharp.Data.JsonProvider to read Twitter Tweets. Playing with this sample code https://github.com/tpetricek/Documents/tree/master/Samples/Twitter.API I want to expand the urls in the tweet with let expandUrl (txt:string) (url:Search.DomainTypes<...>.DomainTypes.Url) = txt.Replace( url.Url, url.ExpandedUrl ) This results in Error: Lookup on object of indeterminate type based on information prior to this program point. A type annotation may be needed prior to this program point to