3
голосов
2ответов
1722 просмотров

F-Sharp (F#) untyped infinity

I wonder why F-Sharp doesn't support infinity. This would work in Ruby (but not in f#): let numbers n = [1 .. 1/0] |> Seq.take(n) -> System.DivideByZeroException: Attempted to divide by zero. I can write the same functionality in much complex way: let numbers n = 1 |> Seq.unfold (...

1
голосов
1ответов
688 просмотров

Calling a F# function via a Linq expression tree MethodCallExpression node?

I am trying to create an expression tree containing a function call to a F# function on a certain module. However, I am missing something because the System.Linq.Expressions.Expression.Call() helper function cant find the function I'm supplying. The Call() call gives an InvalidOperationException...

1
голосов
1ответов
1368 просмотров

Using F#'s built-in async support with *Async() methods

How do I use F#'s built-in support for async operations classes exposing the Event-based Asynchronous Pattern such as WebClient class? let Download(url : Uri) = let client = new WebClient() let html = client.DownloadString(url) html When I try to change this to use "let!" in an asy...

1
голосов
3ответов
208 просмотров

What am I doing wrong in this F# code?

let parallelTest n = Color(Color.DeepPink, Triangles(sphere n));; Parallel.For(0,10,new Action(parallelTest));; Error message : error FS0001: Type mismatch. Expecting a int -> unit but given a int -> scene. The type 'unit' does not match the type 'scene' I'll glad if some body ...

6
голосов
1ответов
863 просмотров

Base a small expression DSL on the DLR or keep it hand-rolled in F#?

I'm building a spreadsheet-like application, where a lot of small calculations needs to be stitched together in a tree-structure. These calculations are user-defined and I need a way for the user to enter them at runtime. My current approach is to write a small "expression DSL" in F#, where I pa...

0
голосов
4ответов
487 просмотров

My first F# program

The idea was to create/populate a perfmon counter with the # of files in a directory. Apparently FileNet (a program) uses a given directory as a queue, and gets hung up sometimes. Hence the need to monitor the # of files that are stacking up... Now most of you will probably LOL your asses off ...

10
голосов
4ответов
1445 просмотров

Extension methods for specific generic types

I'm attempting to create various extension method for a generic type bound to specific generic type parameters in F#, but the language does not seem to be allowing me: What I want to do is something like the following: type IEnumerable<int> with member this.foo = this.ToString...

8
голосов
1ответов
1844 просмотров

Signature Files and Access Modifers in F#

I've recently been trying to learn the Object-Oriented aspects of F#, and have become curious about how to restrict access to types/modules in the language. More specifically, I want to know the difference between writing this: Example.fsi module Stack = val foo : string Example.fs modu...

10
голосов
6ответов
5383 просмотров

F# permutations

I need to generate permutations on a given list. I managed to do it like this let rec Permute (final, arr) = if List.length arr > 0 then for x in arr do let n_final = final @ [x] let rest = arr |> List.filter (fun a -> not (x = a)) Permut...

26
голосов
2ответов
5502 просмотров

How do I create an extension method (F#)?

How do I create an extension method in F#, for example, like this C# extension: public static string Right(this string host, int index) { return host.Substring(host.Length - index); }

7
голосов
5ответов
1533 просмотров

Closest C# equivalent to the F# match expression?

I'm in the situation where a lot of my classes are containers of well-known but unordered objects of different types, e.g. a container may look as follows: public class Container { public A A { get; private set; } public B B { get; private set; } public C C { get; private set; } ...

12
голосов
4ответов
3765 просмотров

F# - Display full results in F# interactive window

Disclaimer: Total F# Newbie question! If I type the following into an F# file in Visual Studio #light let squares = seq { for x in 1 .. 10 -> x * x } printf "%A" squares and run F# interactive on it by highlighting and pressing Alt+Enter, the output in the interactive window is >...

3
голосов
3ответов
1828 просмотров

F#: Filter items found in one list from another list

Say I have two lists: let a = [1 .. 1000] let b = [250 .. 500] How do I get a new list that contains the values {1-249, 501-1000}?

7
голосов
2ответов
1471 просмотров

Extending F# List Module

I've been adding a few handy methods to some of the F# modules such as List. type Microsoft.FSharp.Collections.FSharpList<'a> with //' static member iterWhile (f:'a -> bool) (ls:'a list) = let rec iterLoop f ls = match ls with | head :: tai...

9
голосов
2ответов
505 просмотров

Unit-safe square roots

I just wondered how it is possible to write a user-defined square root function (sqrt) in a way that it interacts properly with F#'s unit system. What it should be like: let sqrt (x : float) = let x' = x / 1.0 // Delete unit (x ** 0.5) * 1.0 // Reassign unit But this is disallowe...

6
голосов
1ответов
288 просмотров

Constructor xml doc in F#?

How do you attach xml documentation (you know, the /// kind) to constructors in F#? I'm not surprised that I haven't managed to figure it out for implicit constructors... but I can't even get it to work for explicit constructors. It works fine for the class itself and other members.

2
голосов
1ответов
262 просмотров

Reusing units of measure across different types

(Yes, still playing with units of measure) I've just started using units of measure for something other than float for the first time. After starting off complicated, I discovered that you can re-use units of measure across multiple 'types'. [<Measure>] type mm let a = 1<mm> let b...

8
голосов
1ответов
854 просмотров

Parallel pipelining

(fileNameToCharStream "bigfile" |>> fuse [length; splitBy (fun x -> x = ' ' || x = '\n') removeEmpty |>> length; splitBy (fun x -> x = '\n') keepEmpty |>> length; ]) (*fuse "fuses" the three functions to run concurrently*) |> run 2 ...

0
голосов
1ответов
743 просмотров

Expression here has type int but is used with type unit

I have the following code: #light let rec fib n = if n=0 || n=1 then 1 //<- here on the "1" n + fib n-1 + fib n-2 How do i fix this? [i've seen this but it didn't help]

12
голосов
1ответов
6802 просмотров

How do I use tryPick to get the first element of a sequence?

I was trying to use Seq.first today, and the compiler says it has been deprecated in favor of Seq.tryPick. It says that it applies a function and returns the first result that returns Some. I guess I can just say fun x -> x!=0 since I know the first one will return Some in my case, but what is...

9
голосов
1ответов
1924 просмотров

The mutable variable 'i' is used in an invalid way.?

I am attempting to write some simple code in F#, and i get this error: Error 1 The mutable variable 'i' is used in an invalid way. Mutable variables may not be captured by closures. Consider eliminating this use of mutation or using a heap-allocated mutable reference cell via 'ref' and '!' ...

6
голосов
9ответов
6668 просмотров

Find common elements in two sorted lists in linear time

I have a sorted list of inputs: let x = [2; 4; 6; 8; 8; 10; 12] let y = [-8; -7; 2; 2; 3; 4; 4; 8; 8; 8;] I want to write a function which behaves similar to an SQL INNER JOIN. In other words, I want to return the cartesian product of x and y which contains only items shared in both lists: jo...

2
голосов
2ответов
630 просмотров

F# equivalent of toLookup

Given this: [ ("A","A122"); ("A","A123"); ("B","B122"); ("B","B123"); ("C","C122"); ] Is there a standard function to get this? [ ("A",["A122";"A123"]); ("B",["B122";"B123"]); ("C",["C122"]) ] I thought of Seq.distinctBy, List.partition, Set, Map, but none of...

15
голосов
7ответов
4364 просмотров

How to handle unit tests in F#?

How do you create unit tests in F#? I typically use the UnitTest portion of Visual Studio with a [TestClass] and [TestMethod] attributes and use the Test View to run these. I know I can just create a script file and run these, but I like the way that it is currently handled.

2
голосов
2ответов
1133 просмотров

How to do chained callbacks in F#?

In C# I am using the asynchronous versions of TcpListener/TcpClient, and I am chaining these via the callback method so that another Accept/Read is posted when the callback completes. Here is an example (untested): public void Start() { TcpListener listener = new TcpListener(IPAd...

5
голосов
1ответов
511 просмотров

How do I write my own async wrapper for TcpListener?

There is no F# async wrapper (in the PowerPack) for neither TcpListener.BeginAcceptTcpClient() nor EndAcceptTcpClient(). How do I write my own wrapper around these, so that I can use the let! and async keywords and run it in parallel?

19
голосов
2ответов
2937 просмотров

Implementing the same interface at different generic instantiations

In C#, I can implement a generic interface twice on one class, using two different type-parameters: interface IFoo<T> { void Foo(T x); } class Bar : IFoo<int>, IFoo<float> { public void Foo(int x) { } public void Foo(float y) { } } I would like to do the same thing i...

7
голосов
4ответов
1195 просмотров

From OO to functional programming at 10,000 feet

I have been using f# and Haskell to learn functional programming for a while now. Until I can get f# approved at our company I must still use c#. I am still trying however to stay in the functional style as I have noticed several benefits. Here is a typical problem. There is a key-set table in...

1
голосов
1ответов
194 просмотров

F# Operator Resolution

I have a class called Vector which implements a number of operators such as + and properties such as Count. Vector is also subtyped by classes such as DenseVector, SparseVector which inherit from Vector. Now in F# when I write the following let foo (v : #Vector) (w : #Vector) = v + w let bar (v ...

100
голосов
10ответов
21316 просмотров

Haskell composition (.) vs F#'s pipe forward operator (|>)

In F#, use of the the pipe-forward operator, |>, is pretty common. However, in Haskell I've only ever seen function composition, (.), being used. I understand that they are related, but is there a language reason that pipe-forward isn't used in Haskell or is it something else?