标签: f#-interactive

fsi.ShowDeclarationValues 如何工作?

根据MSDN文档:

当设置为 false 时,禁用在交互式会话的输出中显示声明值。

然而,以下示例交互式会话似乎与该摘要相矛盾。

> let x = 42;;

val x : int = 42

> fsi.ShowDeclarationValues <- false;;

val it : unit = ()

> let y = 42;;

val y : int
Run Code Online (Sandbox Code Playgroud)

我没想到上面的最后一行。

我是不是误会了什么?谁能确认这是否是一个错误?

谢谢。

f# f#-interactive f#-scripting

3
推荐指数
1
解决办法
309
查看次数

与 fsharp 中的等于运算符进行比较

如果我有下一种类型:

type Color(r: float, g: float, b:float) =
  member this.r = r
  member this.g = g
  member this.b = b
  static member ( * ) (c1:Color, c2:Color) =
      Color (c1.r*c2.r, c1.g*c2.g, c1.b*c2.b)

  static member Zero = Color(0.0,0.0,0.0)
Run Code Online (Sandbox Code Playgroud)

我这样做:

let ca = Color(1.,1.,1.)
let cb = Color(1.,1.,1.)
ca = cb
Run Code Online (Sandbox Code Playgroud)

我应该获得true,但是通过脚本进行的 F# 交互却给了我false 相反,如果我定义为:

let ca = Color(1.,1.,1.)
let cb = ca
ca = cb
Run Code Online (Sandbox Code Playgroud)

它返回true 我尝试以这种方式比较定义类型的两个值是否做错了什么?我怎样才能得到正确的结果?

谢谢

f# f#-interactive

3
推荐指数
1
解决办法
1442
查看次数

f# 类型属性访问

有没有什么办法可以通过使用string. 类似的东西:

type B = {FirstName:string; LastName:string} 
let b = { FirstName="Bob"; LastName="Smith" }  
b.``"FirstName"``
Run Code Online (Sandbox Code Playgroud)

原因是我想根据xml基于地图动态访问类型。

f# f#-interactive f#-3.0

3
推荐指数
1
解决办法
454
查看次数

List.fold和foldBack的渐近时间复杂度

我试图理解这两个函数的时间复杂度.我试过试验这两个,这就是我想出来的

List.foldBack (@) [[1];[2];[3];[4]] [] => [1] @ List.foldBack (@) [[2];[3];[4]] []
=> [1] @ ([2] @ List.foldBack (@) [[3];[4]] [])
=> [1] @ ([2] @ ([3] @ List.foldBack (@) [4] []))
=> [1] @ ([2]@([3] @ ([4] @ List.foldBack[])))
=> [1]@([2]@([3]@([4]@([])))
=> [1; 2; 3; 4]


List.fold (@) [] [[1];[2];[3];[4]]
=> List.fold (@) (([],[1])@ [2]) [[3];[4]]
=> List.fold (@)  ((([]@[1])@[2])@[3]) [[4]]
=> List.fold (@)  (((([]@[1])@[2])@[3])@[4]) []
=> (((([]@[1])@[2])@[3])@[4])
Run Code Online (Sandbox Code Playgroud)

现在在我看来它们都是线性的,因为它需要相同的计算量来实现相同的结果.我是正确还是有些东西我错过了?

algorithm complexity-theory f# f#-interactive

2
推荐指数
1
解决办法
872
查看次数

控制台输入给出虚假值

使用5尝试这个简单的控制台输入后,结果显示为53

printfn "Enter no. of blocks: "
let nBlock = System.Console.Read()
printfn "entered value is %O" nBlock
Run Code Online (Sandbox Code Playgroud)

试图在互动上,仍然得到错误的结果.有解决方案吗?

f# f#-interactive

2
推荐指数
2
解决办法
1326
查看次数

在F#脚本文件中创建C#类实例时出错

我有以下要在F#中使用的C#类

using System;
using System.Collections.Generic;
using System.Text;

namespace DataWrangler.Structures
{
    public enum Type { Trade = 0, Ask = 1, Bid = 2 }

    public class TickData
    {
        public string Security = String.Empty;
        public uint SecurityID = 0;
        public object SecurityObj = null;
        public DateTime TimeStamp = DateTime.MinValue;
        public Type Type;
        public double Price = 0;
        public uint Size = 0;
        public Dictionary<string, string> Codes;
    }
}
Run Code Online (Sandbox Code Playgroud)

我想在F#中创建它的一个实例.我用来执行此操作的代码位于f#脚本文件中

#r @"C:\Users\Chris\Documents\Visual Studio 2012\Projects\WranglerDataStructures\bin\Debug\WranglerDataStructures.dll"

open System
open System.Collections.Generic;
open System.Text;
open DataWrangler.Structures …
Run Code Online (Sandbox Code Playgroud)

syntax f# f#-interactive c#-to-f#

2
推荐指数
1
解决办法
572
查看次数

如何将混合'T和seq <'T>的输入展平为单个seq <'T>

我需要一个可以接受任意数量参数的函数,每个参数可以是类型'Tseq<'T>.在函数内部,我需要将它作为单个处理seq<'T>,所有输入以与它们提供的顺序相同的顺序组合.

显而易见的方法是:

module Test =
    let flatten ([<ParamArray>] args) =
        let flat = seq {
                for a in args do
                    match box a with
                    | :? int as x -> yield x
                    | :? seq<int> as sq -> 
                        for s in sq do
                            yield s
                    | _ -> failwith "wrong input type"
                }
        flat // this should be seq<int>
Run Code Online (Sandbox Code Playgroud)

但即使是最简单的情况,我也无法让它在FSI中发挥作用

let fl = Test.flatten 1;;
  ----------------------^

...: error FS0001: The type 'int' is not …
Run Code Online (Sandbox Code Playgroud)

f# f#-interactive

2
推荐指数
1
解决办法
115
查看次数

匹配数字,如果它是2的倍数

我正在学习f#,目前我正在使用match关键字.我正在修改下一个例子,如果数字是2的倍数则打印到屏幕上,它的mod是0.

[<Literal>]
let Three = 3

let filter123 x =
match x with 
// The following line contains literal patterns combined with an OR pattern.
| 1 | 2 | Three -> printfn "Found 1, 2, or 3!" 
// The following line contains a variable pattern.
| var1 -> printfn "%d" var1

for x in 1..10 do filter123 x
Run Code Online (Sandbox Code Playgroud)

我修改了它并编写了一个额外的匹配:

| x % 2 == 0 -> printfn "it's multiple of 2!"
Run Code Online (Sandbox Code Playgroud)

但这不起作用,它说"%"它是一个未定义的符号......任何想法?谢谢!

f# f#-interactive f#-3.0

2
推荐指数
2
解决办法
400
查看次数

为什么F#interactive没有标记这个错误?

我有几百行的代码.它的许多小块具有以下结构:

let soa =
    election
    |> Series.observations
printfn "%A" <| soa
Run Code Online (Sandbox Code Playgroud)

经常发生两件事:

1)神秘地将最后一行改为:

printfn "%A" <|
Run Code Online (Sandbox Code Playgroud)

以便上面的代码和后面的内容成为

let soa =
    election
    |> Series.observations
printfn "%A" <|

let sls =
    election
    |> Series.sample (seq ["Party A"; "Party R"])
printfn "%A" <| sls
Run Code Online (Sandbox Code Playgroud)

这发生在编辑器中编辑文件的上面数百行.

2)发生这种情况时F# Interactive不会标记错误.不会生成任何错误消息.但是,如果我尝试访问,sls我会收到消息:

error FS0039: The value or constructor 'sls' is not defined.

有关为什么在编辑器中删除了一些代码的想法?(这种情况经常发生)

为什么不F# Interactive发出错误消息?

f# compiler-errors visual-studio f#-interactive

2
推荐指数
1
解决办法
49
查看次数

减去两个Map <'a,int>的地图

我有以下类型:

type Multiset<'a when 'a: comparison> = MSet of Map<'a, int>
Run Code Online (Sandbox Code Playgroud)

我想为这种类型声明一个减去两个MSets的函数.

假设我有以下两个Multisets:

let f = MSet (Map.ofList [("a",1);("b",2);("c",1)])
let g = MSet (Map.ofList [("a",1);("b",3);("c",1)])
Run Code Online (Sandbox Code Playgroud)

我现在尝试创建这个带有两个Multisets的减法函数.

let subtract fms sms =
             match fms with 
             | MSet fs -> match sms with 
                          | MSet ss -> 
                                      let toList ms = Map.fold (fun keys key value -> keys @ [for i = 1 to value do yield key] ) [] ms 
                                      let fromList l = match l with 
                                                       | [] -> MSet(Map.ofList …
Run Code Online (Sandbox Code Playgroud)

f# f#-interactive

2
推荐指数
1
解决办法
147
查看次数