f#

Static extension methods supporting member constraints

99封情书 提交于 2019-12-17 16:59:36
问题 I need to implement a static extension method supporting member constraints on some basic primitive types like integers, floats, etc. Here's my code for signed integers: module MyOperators = let inline foo (x : ^T) = (^T : (static member Foo : ^T -> int) (x)) type System.Int32 with static member Foo(x : Int32) = 7 // static extension Test code: open MyOperators let x = foo 5 // x should be 7 But compiler complains with error: The type 'System.Int32' does not support any operators named 'Foo'

Function Application Operator ($) in F#?

て烟熏妆下的殇ゞ 提交于 2019-12-17 16:55:19
问题 Let's say I have this code let identifier = spaces_surrounded (many1Satisfy isLetter) I was wondering if it there was any native F# function that allowed me to refactor it to let identifier = spaces_surrounded $ many1Satisfy isLetter that is, something such as let ($) f1 f2 = f1 (f2) (that is if I am not mistaken, my Haskell skills are not too sharp..). 回答1: The standard F# idiom for this is the forward pipe operator |> were you would rewrite let identifier = spaces_surrounded (many1Satisfy

F# split sequence into sub lists on every nth element

喜欢而已 提交于 2019-12-17 16:46:19
问题 Say I have a sequence of 100 elements. Every 10th element I want a new list of the previous 10 elements. In this case I will end up with a list of 10 sublists. Seq.take(10) looks promising, how can I repeatedly call it to return a list of lists? 回答1: This is not bad: let splitEach n s = seq { let r = ResizeArray<_>() for x in s do r.Add(x) if r.Count = n then yield r.ToArray() r.Clear() if r.Count <> 0 then yield r.ToArray() } let s = splitEach 5 [1..17] for a in s do printfn "%A" a (* [|1; 2

F# insert/remove item from list

旧时模样 提交于 2019-12-17 16:34:13
问题 How should I go about removing a given element from a list? As an example, say I have list ['A'; 'B'; 'C'; 'D'; 'E'] and want to remove the element at index 2 to produce the list ['A'; 'B'; 'D'; 'E'] ? I've already written the following code which accomplishes the task, but it seems rather inefficient to traverse the start of the list when I already know the index. let remove lst i = let rec remove lst lst' = match lst with | [] -> lst' | h::t -> if List.length lst = i then lst' @ t else

How can evaluate an expression stored as a string in F#

独自空忆成欢 提交于 2019-12-17 16:30:32
问题 I want to do something sort of like this: let x = 5 let y = 10 let expr = Console.ReadLine() expr Where one might type "x+y" in the console to store in expr. How does one evaluate a statement like this in F#? Ultimately, I want a user to be able to enter expressions, or a set of rules for a system, on a webpage and have them saved in a database to be applied at appropriate times in an F# library. I just don't know how to convert the entered string in to a function value in F#. Thanks for any

How do I compute the cartesian product of n sequences in F#?

一个人想着一个人 提交于 2019-12-17 16:28:23
问题 I was given a puzzle as a present. It consists of 4 cubes, arranged side by side. The faces of each cube are one of four colours. To solve the puzzle, the cubes must be orientated so that all four cubes' tops are different, all their fronts are different, all their backs are different and all their bottom's are different. The left and right sides do not matter. My pseudo-code solution was: Create a representation of each cube. Get all the possible orientations of each cube (there are 24 for

Create Discriminated Union Case from String

僤鯓⒐⒋嵵緔 提交于 2019-12-17 16:28:16
问题 I'm trying to create DU cases from strings. The only way I can see doing this is by enumerating over the DU cases via Microsoft.FSharp.Reflection.FSharpType.GetUnionCases and then picking the UnionCase that matches the string (by using .Name ) and then making the actual DU case out of that by using FSharpValue.MakeUnion . Isn't there an easier/more elegant way of doing this? In my scenario I have a DU with a couple of hundred cases for keywords. I have to read the strings (keywords) from a

F# defining/using a type/module in another file in the same project

柔情痞子 提交于 2019-12-17 16:23:47
问题 This will hopefully be an easy one. I have an F# project (latest F# CTP) with two files (Program.fs, Stack.fs). In Stack.fs I have a simple namespace and type definition Stack.fs namespace Col type Stack= ... Now I try to include the namespace in Program.fs by declaring open Col This doesn't work and gives me the error "The namespace or module Col is not defined." Yet it's defined within the same project. I've got to be missing something obvious 回答1: What order are the files in the project?

Implementing phantom types in F#

二次信任 提交于 2019-12-17 16:22:44
问题 Ocaml programmers can use so called 'phantom types' to enforce some constraints using the type system. A nice example can be found at http://ocaml.janestreet.com/?q=node/11. The syntax type readonly doesn't work in F#. It could be replaced with a pseudo-phantom type defined as type readonly = ReadOnlyDummyValue in order to implement the tricks in the above mentioned blog post. Is there a better way to define phantom types in F#? 回答1: I think that defining type just using type somename won't

How can I implement a tail-recursive list append?

爷,独闯天下 提交于 2019-12-17 16:22:03
问题 A simple append function like this (in F#): let rec app s t = match s with | [] -> t | (x::ss) -> x :: (app ss t) will crash when s becomes big, since the function is not tail recursive. I noticed that F#'s standard append function does not crash with big lists, so it must be implemented differently. So I wondered: How does a tail recursive definition of append look like? I came up with something like this: let rec comb s t = match s with | [] -> t | (x::ss) -> comb ss (x::t) let app2 s t =