f#

Shadowing and Nested function

痞子三分冷 提交于 2019-12-17 05:13:33
问题 I want to understand how the mechanism of Shadowing and Nested function work. For example: let func y = let dup y = y + y let z = dup y let dup y = let dup z = let y = y * z y let z = y y dup z + z;; val func : int -> int > func 3;; val it : int = 12 Can someone explain what happen here? 回答1: Your code is equivalent to the following, where I've simply numbered instances of your names to help you visualize how shadowing is occurring. let func y0 = let dup0 y1 = y1 + y1 let z0 = dup0 y0 let

Handy F# snippets [closed]

扶醉桌前 提交于 2019-12-17 03:45:36
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . There are already two questions about F#/functional snippets. However what I'm looking for here are useful snippets, little 'helper'

Handy F# snippets [closed]

强颜欢笑 提交于 2019-12-17 03:45:02
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . There are already two questions about F#/functional snippets. However what I'm looking for here are useful snippets, little 'helper'

Convert tree to list

空扰寡人 提交于 2019-12-14 04:12:42
问题 How can I convert a tree to a list: So far I have the following code but its giving several issues: type 'a tree = Lf | Br of 'a * 'a tree * 'a tree;; let rec elementRight xs = function | LF ->false | Br(m,t1,t2) -> if elementRight xs t1 = false then t1::xs else element xs t1;; //cannot find element let rec elementLeft xs = function | LF ->false | Br(m,t1,t2) -> if elementLeft xs t2 = false then t2::xs else element xs t2 ;; //cannot find element let rec element xs = function | LF ->[] | Br(m

F# 2010 Seq.generate_using

耗尽温柔 提交于 2019-12-14 03:57:41
问题 Is there an alternative/workaround for Seq.generate_using in Visual Studio 2010? The FSharp.PowerPack.dll is not available for 2010 AFAIK 回答1: FYI, the PowerPack binaries for .Net 4.0 Beta1 came online today: http://www.microsoft.com/downloads/details.aspx?FamilyID=e475a670-9596-4958-bfa2-dc0ac29b4631&displaylang=en 回答2: (Sorry about the PowerPack still not being available for 2010 yet.) I don't recall if this already is true of the CTP update, but in internal bits I get the warning: This

F#: Why those two collections are not equal?

白昼怎懂夜的黑 提交于 2019-12-14 03:55:50
问题 Was writing some units using XUnit until that at some points I bumped into something surprising: let id = Guid.Empty let contact = { Name = { FirstName = "Marcel" MiddleInitial = None LastName = "Patulacci" } DateOfBith = new DateTime(1850, 12, 25) Address = { Address1 = "41 av 8 Mai 1945" Address2 = None City = "Sarcelles" State = None Zip = "95200" } PhoneNumber = { DialOutCode = 33 LocalNumber = "766030703" } Email = "marcel.patulacci@outlook.com" } [<Fact>] let ``Open an account...``() =

Multiple Constructors in f# with property assignment

心已入冬 提交于 2019-12-14 03:49:50
问题 I want to have a constructor that is blank and a constructor overload that accepts a parameter and assign it to a public property. This is where I am stuck: type TemplateService() = interface ITemplateService with //Properties member TemplateDirectory = "" //Constructors new (templateDirectory:string) = //Error here. if (templateDirectory == null) then raise (new System.ArgumentNullException("templateDirectory")) TemplateDirectory = templateDirectory; It gives me the error: `Unexpected

How to run code in parallel?

前提是你 提交于 2019-12-14 03:47:43
问题 How can i run these two independent loops simultaneously in parallel. let a1=Seq.map2 (fun a b->(1.0-a)/(a-b)) a2 a3 let b1=Seq.map2 (fun a b->a*b) b2 b3 回答1: You can use standard .NET tasks for this - there are no special F# functions or special syntax for spawning a computation in the background: let a1Work = Task.Factory.StartNew(fun () -> Array.map2 (fun a b->(1.0-a)/(a-b)) a2 a3) let b1 = Array.map2 (fun a b->a*b) b2 b3 let a1 = a1Work.Value I also changed your Seq.map2 to Array.map2 -

How do I compare x and y in F#?

孤街浪徒 提交于 2019-12-14 03:46:17
问题 I need help with the matching pattern that would compare 2 numbers. Something like that: let test x y = match x with | y when x < y -> printfn "less than" | y when x > y -> printfn "greater than" | _ -> printfn "equal" Somehow it falls to the "_" case when x is 0 and y is 200. What am I doing wrong here? 回答1: The problem with your code is that when you write: match x with | y when x < y -> (...) .. it means that you want to assign the value of x (the <expr> in match <expr> with ) to a new

how to get pairs of consecutive values from F# Seq

给你一囗甜甜゛ 提交于 2019-12-14 03:40:59
问题 I have a sequence with {"1";"a";"2";"b";"3";"c";...} . How can I transform this seq into {("1","a");("2","b");("3","c");...} 回答1: Here is a much-too-clever solution: let s = ["1";"a";"2";"b";"3";"c"] let pairs s = s |> Seq.pairwise |> Seq.mapi (fun i x -> i%2=0, x) |> Seq.filter fst |> Seq.map snd printfn "%A" (pairs s) 回答2: Enumerators are not always evil. let pairs (source: seq<_>) = seq { use iter = source.GetEnumerator() while iter.MoveNext() do let first = iter.Current if iter.MoveNext()