f#

F# - Remove duplicate characters after first in string

我们两清 提交于 2019-12-22 08:23:34
问题 What I am trying to do is to remove duplicates of a specific given char in a string but letting the first char to remain. I.e: let myStr = "hi. my .name." //a function that gets a string and the element to be removed in the string someFunc myStr "." where someFunc returns the string showen as below: "hi. my name" It is easy to remove duplicates from a string, but is there a way to remove the duplicates but letting the first duplicated element remain in the string? 回答1: Here's one approach:

Is it possible to define types that depend on each other and are defined in separated files?

独自空忆成欢 提交于 2019-12-22 08:18:54
问题 I am trying to implement a library with extended parsing capabilities. I decided that I will use fsyacc because I knew it from the university. Unfortunately I encountered following problem. I defined a class for the head of my grammar ( Head ) and place its implementation in a single file. Then I defined parser as: ... %start head %type <Head> head ... Fsyacc generates seeparated module ( Parser ). In order to succeed it has to be compiled in following order: Head.fs Parser.fs In order to

Does using a free monad in F# imply a higher startup time and limited instructions?

一曲冷凌霜 提交于 2019-12-22 08:12:47
问题 I am reading this excellent article by Mark Seemann. In it, he gives a simple demonstration of using the free monad to model interactions using pure functions. I understand it enough to be able to write such a program, and I can appreciate the virtues of such an approach. There is one bit of code though, that has me wondering about the implications. let rec bind f = function | Free instruction -> instruction |> mapI (bind f) |> Free | Pure x -> f x The function is recursive. Given that this

F# convert Array2 into a list

流过昼夜 提交于 2019-12-22 08:07:01
问题 I'm still new to functional programming so if I can't figure out how to do something I revert back to procedural style. I found a way to get around having to convert to a list but I'd still like to know how. Here is my attempt to convert a two dimensional array to a list. let board = Array2.init 10 20 (fun i j -> pull(i, j)) let mutable pieces = [] board |> Array2.mapi (fun i j a -> transform(i, j, a)) |> Array2.iter (fun a -> (pieces <- a :: pieces)) 回答1: Apparently in .Net, multi

How to compile a .fs file into a .exe?

為{幸葍}努か 提交于 2019-12-22 08:06:03
问题 I do not find a way to compile a simple file (.fs) to a .exe. I tried this example and it does not work: In the file dolphin.fs: let longBeaked = "Delphinus capensis" let shortBeaked = "Delphinus delphis" let dolphins = [ longBeaked; shortBeaked ] printfn "Known Dolphins: %A" dolphins You can now compile this code to an EXE as shown here: C:\fsharp> fsc dolphins.fs C:\fsharp> dir dolphins.exe But when I do this an error occurs on the ":" from "C:\fsharp" 回答1: This seems a bit wrong - aside

F# WPF: Handling click events in ListBox

好久不见. 提交于 2019-12-22 07:58:33
问题 I'm trying to create a simple task scheduler using F# and WPF. It's basically just a list of tasks where every task has a 'Delete' button. Handling button clicks outside of the list is not a problem -- this can be handled with a regular command. However handling a button click in the list item is not straightforward. I tried using RelayCommand described here with binding routed to parent, but the sender object is always null (I expected it to be the task object from the collection). Also

Using FsLex/Yacc in Vs2013

时间秒杀一切 提交于 2019-12-22 07:48:31
问题 I'm trying to resurrect an old f# parser project I had working in vs 2008 to work with vs 2013. It uses FsLexYacc. I got it building ok by using a prebuild step as thus: fslex --unicode "$(ProjectDir)XpathLexer.fsl" fsyacc --module XpathParser "$(ProjectDir)XpathParser.fsy" But this is less than ideal, as it always executes whether or not the inputs have changed. I then tried just using the old MsBuild actions: <FsYacc Include="XpathParser.fsy"> <FsLex Include="XpathLexer.fsl"> but these

How to resolve FParsec error “The combinator 'many' was applied to a parser that succeeds without consuming…”

故事扮演 提交于 2019-12-22 06:56:37
问题 I have a parser that seems straight-forward enough. I added this sub-parser to the end to give info about general parsing errors since all the other sub-parsers failed - /// Read the rest of a line as an error. let readError = parse { let! restOfLineStr = restOfLine true return makeViolation ("Read error on: " + restOfLineStr + ".") } /// Read an expression. do readExprRef := choice [attempt readBoolean attempt readCharacter attempt readString attempt readInt attempt readError] // just now

using Array.Parallel.map for decreasing running time

孤者浪人 提交于 2019-12-22 06:55:57
问题 Hello everyone I have converted a project in C# to F# that paints the Mandelbrot set. Unfortunately does it take around one minute to render a full screen so I am try to find some ways to speed it up. It is one call that take almost all of the time: Array.map (fun x -> this.colorArray.[CalcZ x]) xyArray xyArray (double * double) [] => (array of tuple of double) colorArray is an array of int32 length = 255 CalcZ is defined as: let CalcZ (coord:double * double) = let maxIterations = 255 let rec

How would you implement a beta-reduction function in F#?

孤街醉人 提交于 2019-12-22 06:35:14
问题 I am writing a lambda calculus in F#, but I am stuck on implementing the beta-reduction (substituting formal parameters with the actual parameters). (lambda x.e)f --> e[f/x] example of usage: (lambda n. n*2+3) 7 --> (n*2+3)[7/n] --> 7*2+3 So I'd love to hear some suggestions in regards to how others might go about this. Any ideas would be greatly appreciated. Thanks! 回答1: Assuming your representation of an expression looks like type expression = App of expression * expression | Lambda of