f#

Emulating Prolog backtracking in F#

≯℡__Kan透↙ 提交于 2019-12-09 18:15:20
问题 I am currently involved in a project to develop an application able to consider a set of nodes and connections and find the shortest path (a common and well-known issue) between two nodes (on allowed connections). Well I don't really have to build an application from zero, but just need to "convert" a Prolog pre-existing application in f#. I thought I bit about it and finally asked myself one question: "Instead of developing a special purpose solution and implementing new algorithms

How should I modify my Queue class to allow users to create empty queues of unspecified type in F#?

帅比萌擦擦* 提交于 2019-12-09 18:14:39
问题 I have created an immutable Queue in F# as follows: type Queue<'a>(f : 'a list, r : 'a list) = let check = function | [], r -> Queue(List.rev r, []) | f, r -> Queue(f, r) member this.hd = match f with | [] -> failwith "empty" | hd :: tl -> hd member this.tl = match f, r with | [], _ -> failwith "empty" | hd::f, r -> check(f, r) member this.add(x) = check(f, x::r) static member empty : Queue<'a> = Queue([], []) I want to create an instance of an empty Queue , however I get a value-restriction

Steps and involvement of implementing a parser (in .Net - and in this case XPath 2.0)

雨燕双飞 提交于 2019-12-09 18:10:41
问题 In the lack of any good free XPath 2.0 implementations for .Net build upon Linq to XML I have thought about implementing my own (also for the experience). But just to be clear (and not building something that exists) these are the XPath 2.0 implementations I have found: Saxon .Net Query Machine - I had problems with this - exceptions with the examples XQSharp - may be good, but is commercial (single developer ~300 $) Now, I want some thoughts on how difficult it is to implementing some

Another limitation of F# quotations?

点点圈 提交于 2019-12-09 16:28:11
问题 Earlier today I encountered a limitation of F# quotations, and asked a question about it here: F# quotations: variable may escape scope Now, I may have encountered another limitation when converting examples appearing in http://www.cs.rice.edu/~taha/publications/journal/dspg04a.pdf from MetaOcaml to F#. This time I've this MetaOcaml snippet: let rec peval2 p env fenv= match p with Program ([],e) -> eval2 e env fenv | Program (Declaration (s1,s2,e1)::tl,e) -> .<let rec f x = .~(eval2 e1 (ext

Fold / Recursion over Multiway Tree in f#

瘦欲@ 提交于 2019-12-09 16:24:14
问题 I am trying to adapt Brian's Fold for Bianary Trees (http://lorgonblog.wordpress.com/2008/04/06/catamorphisms-part-two/) to apply for Multiway trees. Summarizing from Brian's Blog: Data structure: type Tree<'a> = | Node of (*data*)'a * (*left*)Tree<'a> * (*right*)Tree<'a> | Leaf let tree7 = Node(4, Node(2, Node(1, Leaf, Leaf), Node(3, Leaf, Leaf)), Node(6, Node(5, Leaf, Leaf), Node(7, Leaf, Leaf))) Binary Tree Fold function let FoldTree nodeF leafV tree = let rec Loop t cont = match t with |

Infinite fibonacci sequence

僤鯓⒐⒋嵵緔 提交于 2019-12-09 16:17:57
问题 I'm trying to imitate Haskell's famous infinite fibonacci list in F# using sequences. Why doesn't the following sequence evaluate as expected? How is it being evaluated? let rec fibs = lazy (Seq.append (Seq.ofList [0;1]) ((Seq.map2 (+) (fibs.Force()) (Seq.skip 1 (fibs.Force()))))) 回答1: The problem is that your code still isn't lazy enough: the arguments to Seq.append are evaluated before the result can be accessed, but evaluating the second argument ( Seq.map2 ... ) requires evaluating its

Best practices to parallelize using async workflow

半城伤御伤魂 提交于 2019-12-09 16:08:24
问题 Lets say I wanted to scrape a webpage, and extract some data. I'd most likely write something like this: let getAllHyperlinks(url:string) = async { let req = WebRequest.Create(url) let! rsp = req.GetResponseAsync() use stream = rsp.GetResponseStream() // depends on rsp use reader = new System.IO.StreamReader(stream) // depends on stream let! data = reader.AsyncReadToEnd() // depends on reader return extractAllUrls(data) } // depends on data The let! tells F# to execute the code in another

Does F# have equivalent syntax to C#'s “unsafe” block

断了今生、忘了曾经 提交于 2019-12-09 15:37:15
问题 A lot of array boundary checking slows down the speed, this is especially true to 2D arrays. Is there a way to write unsafe code blocks in F#? 回答1: I'm not a F# programmer but as far as I can see it doesn't seem to have the unsafe keyword. You could possibly get a performance boost by transforming the 2D array to an one-dimensional array. Advice 5: Until we get this right, I would suggest that .NET users do what many C++ numerical programmers do: write a class to implement your n-dimensional

why Seq.iter is 2x faster than for loop if target is for x64?

时光怂恿深爱的人放手 提交于 2019-12-09 15:09:57
问题 Disclaim: This is micro-benchmark, please do not comment quotes such as "premature optimization is evil" if you feel unhappy about the topic. Examples are release targeted for x64, .Net4.5 Visual Studio 2012 F# 3.0 and run in windows 7 x64 After profiling, I narrowed down the bottleneck of one of my applications, so that I want to raise this question: Observation If there is no loop inside for in loop or Seq.iter , then it is clear they are both of similar speed. (update2 vs update4) If there

What are the similarities / differences between Control.Observable and Control.Event modules in F#?

前提是你 提交于 2019-12-09 14:57:34
问题 F# (at least in Visual Studio 2012) has both Control.Observable and Control.Event. How are they related? Which one should be used when? Are there performance differences between the two? I would also love to know what Haskell modules / packages / features the .NET IEnumerable / IObservable duality achieved with reactive extensions to .NET correspond to. 回答1: To answer the first part of your question, there is a number of differences between IEvent and IObservable . The reason why there are