标签: f#-interactive

键入不匹配错误.F#类型推断失败?

我正在尝试在F#中编写一个方法,该方法根据传递给方法的值的类型返回泛型类型的新实例.在FSI:

 open System.Collections.Generic

 type AttributeIndex<'a>() = 
    inherit SortedDictionary<'a, HashSet<int array>>()

 let getNewIndexForValue (value: obj) : AttributeIndex<_> =
    match value with
      | :? string -> new AttributeIndex<string>()
      | :? int -> new AttributeIndex<int>()
      | :? float -> new AttributeIndex<float>()
      | :? bool -> new AttributeIndex<bool>()
      | _ -> failwith "bad value type"

 let someIndexes = [
    getNewIndexForValue 9;
    getNewIndexForValue "testString";
    getNewIndexForValue false;
    getNewIndexForValue 5.67;
 ]

 someIndexes;;
Run Code Online (Sandbox Code Playgroud)

这不会编译错误

error FS0001: Type mismatch. Expecting a AttributeIndex<string>
but given a AttributeIndex<int>
The type 'string' …

.net f# functional-programming type-inference f#-interactive

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

F# 脚本/F# 交互中的性能调整

我需要将任意整数(即bigint)转换为其数字,以便我可以通过索引访问它们。

不过,我发现自己在该算法的两种可能实现之间徘徊:

open System

let toDigits (x:bigint) =
    x.ToString() 
    |> Seq.map (fun c -> (int c) - (int '0'))
    |> Seq.toArray

let toDigits' (x:bigint) =
    seq {
        let x' = ref x
        while !x' <> 0I do
            yield (int (!x' % 10I))
            x' := !x' / 10I
    } |> Seq.toArray |> Seq.rev
Run Code Online (Sandbox Code Playgroud)

我嘀咕哪一个最快?为了帮助我回答这个问题,我设计了一个简单的profile方法

let profile f times = 
    let x = ref 0
    while !x < times do
        incr x
        f (bigint !x) |> ignore
Run Code Online (Sandbox Code Playgroud)

与 …

.net performance f# profiling f#-interactive

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

为什么F#Interactive在排序方面不同于Console运行?

我对F#interactive中的输出和Console上的输出之间的区别感到困惑.

问题概述:

给定长度为n的数组A按以下顺序对索引进行排序:

index[i] < index[j] if (A[i] < A[j]) or (A[i] = A[j] and i < j)

例如

[| 4; 2; 4; 2; 1; 1 |]  
Run Code Online (Sandbox Code Playgroud)

这里的索引会像这样排序

[| 4; 5; 1; 3; 0; 2 |]  


    let GetSorted (A:int[]) =
    let Comparer i j =
        match (A.[i], A.[j]) with
            | a1, a2 when a1 > a2 -> +1
            | a1, a2 when a1 < a2 -> -1
            | _, _ when i > j -> +1
            | _, _ -> …
Run Code Online (Sandbox Code Playgroud)

arrays sorting f# f#-interactive

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

F#:阻止此'let'未完成.期待一个表达

我知道在F#中我们应该将每个值绑定到一个名称.我觉得我的确不错???

但是在if语句中我有以下错误.

Block following this 'let' is unfinished. Expect an expression
Run Code Online (Sandbox Code Playgroud)

它来自于 min= List.nth list i.据我所知,我所界定的分钟List.nth list i.那么为什么它应该是一个错误?

let mutable list =[-1;2;3;4]
let mutable min=list.[0]
let mutable i=1

if min<=0  then   let  min= List.nth list i  
Run Code Online (Sandbox Code Playgroud)

f# f#-interactive

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

这段代码是如何工作的?

我刚刚面对以下代码.

let rec Make_Segment list=
  match list with
  | []  ->[],[]
  | hd::tail when hd<=tail.Head -> let current,rest=Make_Segment tail
                                   hd::current,rest
  | hd::tail when hd>tail.Head -> [hd], tail

let segments= [3;4;5;5;1;2;3]

Make_Segment segments 
Run Code Online (Sandbox Code Playgroud)

它是一个递归函数,总是返回两个列表.但正如您所看到的,它始终使用一个参数,即list匹配中的参数.那么第一个论点在哪里?

它怎么样?

f# f#-interactive

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

#if - #else - #endif 破坏 F# 脚本

使用 Visual Studio 2015 Update 3 和fsi.exeF# v4.0,我尝试运行此脚本:

//error.fsx

#if INTERACTIVE
    let msg = "Interactive"
#else
    let msg = "Not Interactive"
#endif

let add x y =
    x + y

printfn "%d" (add 1 2)
Run Code Online (Sandbox Code Playgroud)

输出: error.fsx(12,15):错误 FS0039:未定义值或构造函数“add”

如果我然后注释掉#if- #else-#endif块,它工作正常:

// fixed.fsx

//#if INTERACTIVE
//    let msg = "Interactive"
//#else
//    let msg = "Not Interactive"
//#endif

let add x y =
    x + y

printfn "%d" (add 1 2)
Run Code Online (Sandbox Code Playgroud)

输出: 3

我确定我做错了什么(而不是这是一个错误),但我一生都无法弄清楚如何进行这项工作。 …

f# f#-interactive f#-scripting f#-4.0

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

F#:创建大小为n的列表时出错

我正在尝试从更大的已创建列表中创建一个大小为n的元素列表.我收到一条错误消息:此值不是函数,无法应用.不完整的模式匹配此表达式.例如,值"[]"可以指示模式未涵盖的情况.在7,16

谁能帮忙看看我做错了什么?另外,我正在努力理解F#,所以我真的不想要任何可以做我要求的东西,除非它是一个FYI的东西.但我仍然需要帮助创建一个功能来做到这一点.

//create a list of size n from beginning of a dataset
let populateList n =
    let starterList = []
    let data = [1;2;3;4;5;6;7;8]
    let rec helper count aList = 
        let head::tail = aList
        if count < k then head :: helper count+1 tail else []


    helper 0 data

populateList 3
Run Code Online (Sandbox Code Playgroud)

f# f#-interactive

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

从F#上的元组列表中创建一个列表

可以说我有一个元组列表.只是为了更容易引用,它是一个带有x和y值的坐标.

let test = [(1,34);(2,43);(3,21);(1,51);(2,98);(3,56);(1,51)]

我想使用test创建另一个列表,这样如果我只想要x值为1的值,它将返回[34; 51; 51]

f# f#-interactive

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

在 F# 交互窗口中显示完整字符串

如果我在 Visual Studio Code 中的 fsx 中输入以下内容

let longString = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
Run Code Online (Sandbox Code Playgroud)

Alt …

f# f#-interactive visual-studio-code vscode-debugger

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

在 F# 中可以使用 %A 创建一个速记函数来打印吗

我刚刚开始第一次使用 F#,使用 VSCode 和交互式笔记本。我对不得不不断地写出来感到非常恼火

printfn "%A" something
Run Code Online (Sandbox Code Playgroud)

因为它杀死了我的内心。

在每个文件的开头简单地写:

let print(something) = printfn "%A" something

// then use with

print(4 + 3) // int
print(3.7 + 1.1) //float
print("this is so much better") // text
Run Code Online (Sandbox Code Playgroud)

为什么这个不内置?

f# f#-interactive visual-studio-code

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