f#

Difference in F# and Clojure when calling redefined functions

时光毁灭记忆、已成空白 提交于 2019-12-22 03:51:35
问题 In F#: > let f x = x + 2;; val f : int -> int > let g x = f x;; val g : int -> int > g 10;; val it : int = 12 > let f x = x + 3;; val f : int -> int > g 10;; val it : int = 12 In Clojure: 1:1 user=> (defn f [x] (+ x 2)) #'user/f 1:2 user=> (defn g [x] (f x)) #'user/g 1:3 user=> (g 10) 12 1:4 user=> (defn f [x] (+ x 3)) #'user/f 1:5 user=> (g 10) 13 Note that in Clojure the most recent version of f gets called in the last line. In F# however still the old version of f is called. Why is this

Additional constructors in F#

左心房为你撑大大i 提交于 2019-12-22 03:43:45
问题 I am trying to create an additional constructor in F# that does some extra work (i.e. reads a basic csv file) as follows: type Sheet () = let rows = new ResizeArray<ResizeArray<String>>() let mutable width = 0 new(fileName) as this = Sheet() then let lines = System.IO.File.ReadLines fileName for line in lines do let cells = line.Split ',' rows.Add(new ResizeArray<String> (cells)) //line 16 if cells.Length > width then width <- cells.Length but I get the following errors: Error 1 The namespace

Bitmap.SetPixel acts slower in f# than in c#

…衆ロ難τιáo~ 提交于 2019-12-22 03:24:46
问题 The f# code goes literally 500 times slower than the c# code. What am I doing wrong? I tried to make the code basically the same for both languages. It doesn't make sense that SetPixel would be that much slower in f#. F#: module Imaging open System.Drawing; #light type Image (width : int, height : int) = class member z.Pixels = Array2D.create width height Color.White member z.Width with get() = z.Pixels.GetLength 0 member z.Height with get() = z.Pixels.GetLength 1 member z.Save (filename

F# record member evaluation

非 Y 不嫁゛ 提交于 2019-12-22 03:22:50
问题 Why is t.b evaluated on every call? And is there any way how to make it evaluate only once? type test = { a: float } member x.b = printfn "oh no" x.a * 2. let t = { a = 1. } t.b t.b 回答1: It's a property; you're basically calling the get_b() member. If you want the effect to happen once with the constructor, you could use a class: type Test(a:float) = // constructor let b = // compute it once, store it in a field in the class printfn "oh no" a * 2. // properties member this.A = a member this.B

How to set default argument value in F#?

折月煮酒 提交于 2019-12-22 03:22:41
问题 Take this function as an example: // Sequence of random numbers open System let randomSequence m n= seq { let rng = new Random() while true do yield rng.Next(m,n) } randomSequence 8 39 The randomSequence function takes two arguments: m, n . This works fine as a normal function. I would like to set the default for m, n , for example: (m = 1, n = 100) When there's no arguments given, the function take the default value. Is it possible in F#? 回答1: You can often achieve the same effect as

Set of keys from a map

会有一股神秘感。 提交于 2019-12-22 02:02:42
问题 I have a map X and I'm trying to get a set of the keys satisfying a certain condition, something like this: Map.Keys X |> Set.filter (fun x -> ...) ...but I cannot find the way to get the keys from F#'s Map collection. 回答1: Convert your map to sequence of tuples (key,value) first and then map it to a sequence of just keys: map |> Map.toSeq |> Seq.map fst FSI sample: >Map.ofList[(1,"a");(2,"b")] |> Map.toSeq |> Seq.map fst;; val it : seq<int> = seq [1; 2] Or alternatively, as ordering of keys

Why is use better than using?

故事扮演 提交于 2019-12-22 01:37:42
问题 According to the last sentence on this MSDN page use is to be preferred over using . I've heard it elsewhere (this answer, for example). Why is this? I realize use was added later. But what's the difference? On the surface, using seems more useful because you can control when Dispose() is called, and you can explicitly ignore the bound value (e.g., (fun _ -> ...) ) if needed. 回答1: I think that the reason for preferring use is just that the syntax is simpler. Many other language constructs

What are the benefits of using Scala in .Net?

会有一股神秘感。 提交于 2019-12-22 01:28:21
问题 Scala is a peculiar programming language in that it targets both JVM and the CLR. But what are the benefits? Is it worth considering it as a viable alternative to the F# language? 回答1: Most of the web search hits I see from "Scala .NET" are from 2008 or early 2009. Scala is an interesting language, but my admittedly-ill-informed impression is that is is not a "viable alternative" for an industrial-strength .NET application. I see people talk in passing about writing apps that are portable

F# non-trivial non-primary constructor

北城以北 提交于 2019-12-21 22:24:52
问题 I want to do some work in a non-primary constructor before calling the primary constructor, e.g. something like this: type Foo(a:int,b:int) = let a = a let b = b new(s:string) = //...work here (intermediate let bindings) let _a = ... let _b = ... Foo(_a,_b) If possible, how can I achieve this (now that I think about it, I'm not even sure if this can be done in C#, but the goal is similar to how you can call base constructors anywhere you like in an extending class constructor... but I don't

F# How to tokenise user input: separating numbers, units, words?

扶醉桌前 提交于 2019-12-21 20:26:40
问题 I am fairly new to F#, but have spent the last few weeks reading reference materials. I wish to process a user-supplied input string, identifying and separating the constituent elements. For example, for this input: XYZ Hotel: 6 nights at 220EUR / night plus 17.5% tax the output should resemble something like a list of tuples: [ ("XYZ", Word); ("Hotel:", Word); ("6", Number); ("nights", Word); ("at", Operator); ("220", Number); ("EUR", CurrencyCode); ("/", Operator); ("night", Word); ("plus",