f#

Multiplying a list of tuples by a tuple in F#

风格不统一 提交于 2019-12-23 18:10:24
问题 I'm writing some code in F# and I need to multiply and add each element in list of tuples (c,d) by a tuple (a,b) . To clarify, I have some tuple (a,b) And some list of tuples [(c,d),(e,f),(g,h)...] and I'm trying to get [(a*c,b*d),(a*e,b*f),(a*g,b*h)...] and I'm trying to get (with a separate function) [(a+c,b+d),...] I have tried to use List.map to multiply each element in the list by a tuple but I get an error that * is an invalid operator for type tuple. This is how I ended up implementing

F# try with unhandled exceptions

烈酒焚心 提交于 2019-12-23 18:07:33
问题 In the following code, I want to read a file and return all lines; if there is IO error, I want the program exit with error message print to console. But the program still run into unhandled exception. What's the best practice for this? (I guess I dont need Some/None since I want to the program exit at error anyway.) thanks. let lines = try IO.File.ReadAllLines("test.txt") with | ex -> failwithf " %s" ex.Message 回答1: You can do type test pattern matching. let lines = try IO.File.ReadAllLines(

How do I resolve SecureChannelFailure on OSX with mono

﹥>﹥吖頭↗ 提交于 2019-12-23 17:56:42
问题 I'm trying to access https://geocoder.cit.api.here.com with .NET (F#) on mono. I'm using the Http helper from FSharp.Data using the following code: let baseUrl = "https://geocoder.cit.api.here.com/6.2/geocode.json?searchtext=1701%20McFarland%20Blvd%20E%20Tuscaloosa%09%20AL&app_id=<Some app id>&app_code=<Some app code>" let res = Http.RequestString(baseUrl) Running this for me on OSX Sierra (10.12.2) with mono version 4.6.2 yields a SecureChannelFailure . How do I resolve this? I have tried

How to count the occurrence of a specific character in a string

筅森魡賤 提交于 2019-12-23 17:37:35
问题 I'm not sure where to begin with the checking of a string for n occurrences of a specific char. I've laid out the basic outline of what I assume to be the framework for the function but the contents I'm not sure about? let countCharFromNth (getStr : string)(chkdChar : char) = if getStr.Length >=1 then else printfn "Not enough arguments" 回答1: Here is a tail recursive loop version. let countCharFromNth (getStr : string)(chkdChar : char) = let rec loop i count = if i < getStr.Length then if

Behaviour of greater than (and equivalent) for Option

孤街醉人 提交于 2019-12-23 17:24:07
问题 I'm working on something for an image processing task (it's not greatly relevant here), and I stumbled across behaviour of F#'s Option type that surprised me, regarding performing greater than (>) comparisons. I couldn't find anything that directly explained what I should expect (more on that below) on Stack Overflow, the F# docs or the wider web. The specific part I am looking at looks something like: let sort3Elems (arr: byte option []) = if arr.[0] > arr.[1] then swap &arr.[0] &arr.[1] if

Error FS0037 sometimes, very confusing

时光总嘲笑我的痴心妄想 提交于 2019-12-23 16:35:52
问题 If I write the following F# code, the compiler issues an error. let a = 123 let a = 123 The error produced is: error FS0037: Duplicate definition of value 'a' If I write the same code in a function like this: let fctn = let a =123 let a =123 a it doesn't produce any error. I don't understand the difference. Can anyone please explain? Edit : first code I write in module level. 回答1: I agree this is confusing. The problem is that let behaves differently when it is used as a local variable

Object initializer + property initializer (from C# to F#)

萝らか妹 提交于 2019-12-23 16:27:47
问题 I have a Person and I want initialize the Name with the property initializer and the Age with the constructor. C# version public class Person { public string Name { get; set; } public int Age { get; set; } public Person(int age) { Age = age } } var person = new Person(20) { Name = "Alex" }; I've tried with F#: Try 1: Invalid syntax type Person = { Name: string Age: int } with static member create (age: int): Person = { this with Age = age }: Person Try 2: Invalid syntax type Person = member

Add calculated key to collection

穿精又带淫゛_ 提交于 2019-12-23 16:24:45
问题 Please consider this dataset, composed by man and woman, and that I filter in a second moment according to few variables: type ls = JsonProvider<"..."> let dt = ls.GetSamples() let dt2 = dt |> Seq.filter (fun c -> c.Sex = "male" && c.Height > Some 150) dt2 [{"sex":"male","height":180,"weight":85}, {"sex":"male","height":160" "weight":60}, {"sex":"male","height":180,"weight":85}] Lets suppose that I would like to add a fourth key "body mass index" or "bmi", and that its value is roughly given

This expression was expected to have type string -> int but here has type int

♀尐吖头ヾ 提交于 2019-12-23 16:19:51
问题 Sorry if this question is really silly, just started to get my feet wet with F# (Coming from C#). Suppose I have following: 1: let Foo (names : string[]) = 2: let favNames : string[] = 3: Array.filter(fun name -> name.StartsWith("M")) names 4: let sortByLengthOfName (x : string) : int = x.Length 5: let sortedNamesByLegth : string[] = 6: Array.sortWith(fun name -> fun n -> n.Length) favNames 7: Array.iter(fun name -> printfn "%s" name) sortedNamesByLegth Here I'm trying to define/(declare?) a

F# Data: JSON Parser. Using JsonExtensions

核能气质少年 提交于 2019-12-23 16:19:17
问题 It is my first question on SO...so do not judge strictly =) Usually all my questions techout in chat rooms (believe me, a lot of them =)). Recently, we are talking about the RosettaCode. And I wondered to complement some of the tasks code to F# One of them is JSON. One of the possible solutions is the use of "F# Data: JSON Parser". So my question is linked with it. This code works well: open FSharp.Data open FSharp.Data.JsonExtensions type Person = {ID: int; Name:string} let json = """[ { "ID