标签: f#-interactive

错误FS0752在函数列表中的映射的F#声明中

我想在相应值列表上执行函数列表:

let f1 x = x*2;;  
let f2 x = x+70;;

let conslist = [f1;f2];;

let pmap2 list1 list2 =   
  seq { for i in 0..1 do yield async { return list1.[i] list2.[i] } }  
  |> Async.Parallel  
  |> Async.RunSynchronously;;  
Run Code Online (Sandbox Code Playgroud)

结果:

  seq { for i in 0..1 do yield async { return list1.[i] list2.[i] } }
----------------------------------------------^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

stdin(213,49):错误FS0752:运算符'expr.[idx]'已被用作基于此程序点之前的信息的不确定类型的对象.考虑添加其他类型约束

我想执行:pmap2 conslist [5; 8] ;; (在平行下)

f# f#-interactive

4
推荐指数
1
解决办法
757
查看次数

笛卡尔积两个列表

可能重复:
F# - 两个列表的交叉乘积
在F#中有效地投影列表列表

我有一个函数,它接受两个整数列表,并返回一个包含所有笛卡儿积的列表.我认为我有正确的想法,但没有正确的实施.我可以得到一些指示吗?

let rec cartesian = function
 | ([],[]) -> []
 | (xs,[]) -> []
 | ([],ys) -> []
 | (x::xs,ys) ->   List.map(fun y -> (x,y)::[]) cartesian (xs,ys) 
Run Code Online (Sandbox Code Playgroud)

f# f#-interactive f#-scripting

4
推荐指数
1
解决办法
4868
查看次数

禁止在F#interactive中显示变量内容

可能重复:
F# - 保持F#交互发布输出

有没有办法如何抑制在F#interactive中显示变量的内容?

我为什么要这样?这是仅为F#interactive中的一个调用打印出来的内容.要找到任何有趣的信息,例如printfn,这真的很痛苦,而且打印的大量数据对我来说毫无用处......

Microsoft (R) F# Interactive version 11.0.50727.1
Copyright (c) Microsoft Corporation. All Rights Reserved.

For help type #help;;

> 

--> Referenced 'C:\Olda\Project_Root\Computations\Oldrich\2012-Articles\07-FiberFit-2012\Solution\PostProcess\.\bin\Release\LBM.dll'


--> Referenced 'C:\Olda\Project_Root\Computations\Oldrich\2012-Articles\07-FiberFit-2012\Solution\PostProcess\.\bin\Release\Utilities.dll'

find me

val dir : string = "2012-08-30-LOCylinder-NoFit-A"
val root : string =
  "y:\Olda\Project\Computations\Oldrich\2012-Articles\07-FiberFi"+[65 chars]
val dirA : string [] =
  [|"y:\Olda\Project\Computations\Oldrich\2012-Articles\07-FiberFi"+[119 chars];
    "y:\Olda\Project\Computations\Oldrich\2012-Articles\07-FiberFi"+[117 chars];
    "y:\Olda\Project\Computations\Oldrich\2012-Articles\07-FiberFi"+[120 chars];
    "y:\Olda\Project\Computations\Oldrich\2012-Articles\07-FiberFi"+[120 chars];
    "y:\Olda\Project\Computations\Oldrich\2012-Articles\07-FiberFi"+[119 chars];
    "y:\Olda\Project\Computations\Oldrich\2012-Articles\07-FiberFi"+[117 chars];
    "y:\Olda\Project\Computations\Oldrich\2012-Articles\07-FiberFi"+[117 chars];
    "y:\Olda\Project\Computations\Oldrich\2012-Articles\07-FiberFi"+[120 chars];
    "y:\Olda\Project\Computations\Oldrich\2012-Articles\07-FiberFi"+[118 chars];
    "y:\Olda\Project\Computations\Oldrich\2012-Articles\07-FiberFi"+[114 chars];
    "y:\Olda\Project\Computations\Oldrich\2012-Articles\07-FiberFi"+[116 chars];
    "y:\Olda\Project\Computations\Oldrich\2012-Articles\07-FiberFi"+[118 chars];
    "y:\Olda\Project\Computations\Oldrich\2012-Articles\07-FiberFi"+[118 chars];
    "y:\Olda\Project\Computations\Oldrich\2012-Articles\07-FiberFi"+[116 chars];
    "y:\Olda\Project\Computations\Oldrich\2012-Articles\07-FiberFi"+[116 …
Run Code Online (Sandbox Code Playgroud)

f# f#-interactive suppressmessage

4
推荐指数
1
解决办法
783
查看次数

位图图像处理

我想使用 LockBits 方法替换 GetPixel 和 SetPixel,所以我遇到了这个F# 延迟像素读取

open System.Drawing
open System.Drawing.Imaging

let pixels (image:Bitmap) =
    let Width = image.Width
    let Height = image.Height
    let rect = new Rectangle(0,0,Width,Height)

    // Lock the image for access
    let data = image.LockBits(rect, ImageLockMode.ReadOnly, image.PixelFormat)

    // Copy the data
    let ptr = data.Scan0
    let stride = data.Stride
    let bytes = stride * data.Height
    let values : byte[] = Array.zeroCreate bytes
    System.Runtime.InteropServices.Marshal.Copy(ptr,values,0,bytes)

    // Unlock the image
    image.UnlockBits(data)

    let pixelSize = 4 // <-- calculate this …
Run Code Online (Sandbox Code Playgroud)

f# f#-interactive f#-3.0

4
推荐指数
1
解决办法
1940
查看次数

我可以在StructuredFormatDisplayAttribute中使用多个属性吗?

我正在玩StructuredFormatDisplay,我假设我可以使用多个属性Value,但似乎并非如此.这个问题(和接受的答案)一般都谈到定制,但给出的例子只使用一个属性.在使用此属性时,MSDN没有帮助.

这是我的例子:

[<StructuredFormatDisplay("My name is {First} {Last}")>]
type Person = {First:string; Last:string}
Run Code Online (Sandbox Code Playgroud)

如果我然后尝试这个:

let johnDoe = {First="John"; Last="Doe"}
Run Code Online (Sandbox Code Playgroud)

我最终得到了这个错误:

<StructuredFormatDisplay exception: Method 'FSI_0038+Person.First} {Last' not found.>

这个错误似乎暗示它只捕获了我提到的第一个属性,Value但我很难说有信心.

我已经发现我可以通过声明我的类型来解决这个问题:

[<StructuredFormatDisplay("My name is {Combined}")>]
type Person = {First:string; Last:string} with
    member this.Combined = this.First + " " + this.Last
Run Code Online (Sandbox Code Playgroud)

但我想知道是否有人可以解释为什么我不能使用多个属性,或者如果可以的话,我缺少什么语法.

我在源头做了一些挖掘并发现了这个评论:

在此版本的F#中,唯一有效值的格式为PreText {PropertyName} PostText

但是我无法找到实际实现该限制的地方,所以也许更熟悉代码库的人可以简单地指出我实施这个限制的地方并且我承认失败.

f# string-formatting f#-interactive

4
推荐指数
1
解决办法
338
查看次数

Mac 上的 F# Interactive 在哪里

在Windows中,F#的互动和F#编译器可执行文件被命名为fsifsc。在带有 Mono 的 Mac 上,它们被称为fsharpifsharpc。这是为什么?

macos mono f# f#-interactive

4
推荐指数
1
解决办法
1465
查看次数

F# CSV TypeProvider 在控制台应用程序中不太健壮

我正在尝试使用来自冠状病毒大流行的实时数据(不幸的是,祝我们所有人好运)。

我开发了一个小脚本,我正在过渡到一个控制台应用程序:它使用 CSV 类型的提供程序。

我有以下问题。假设我们要按地区过滤意大利传播,我们可以将此代码用于 .fsx 文件:

open FSharp.Data

let provinceData = CsvProvider< @"https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-province/dpc-covid19-ita-province.csv" , IgnoreErrors = true>.GetSample()


let filterDataByProvince province = 
    provinceData.Rows
    |> Seq.filter (fun x -> x.Sigla_provincia = province)
Run Code Online (Sandbox Code Playgroud)

由于序列懒惰,然后假设我强制编译器将罗马省的数据加载到内存中,我可以添加:

let romeProvince = filterDataByProvince "RM" |> Seq.toArray
Run Code Online (Sandbox Code Playgroud)

这工作正常,由 FSI 在本地运行。

现在,如果我使用 .fs 文件将此代码转换为控制台应用程序;我声明了完全相同的函数并使用完全相同的类型提供程序加载器;但我没有使用最后一行来收集数据,而是将其放入主函数中:

[<EntryPoint>]
let main _ =
    let romeProvince = filterDataByProvince "RM" |> Seq.toArray

    Console.Read() |> ignore
    0
Run Code Online (Sandbox Code Playgroud)

这会导致以下运行时异常:

System.Exception
  HResult=0x80131500
  Message=totale_casi is missing
  Source=FSharp.Data
  StackTrace:
   at <StartupCode$FSharp-Data>.$TextRuntime.GetNonOptionalValue@139-4.Invoke(String message)
   at CoronaSchiatta.Evoluzione.provinceData@10.Invoke(Object parent, String[] row) in C:\Users\glddm\source\repos\CoronaSchiatta\CoronaSchiatta\CoronaEvolution.fs:line 10 …
Run Code Online (Sandbox Code Playgroud)

f# f#-interactive type-providers f#-data fsharp.data.typeproviders

4
推荐指数
1
解决办法
188
查看次数

为什么 Async.Sequential 不能按预期工作(但在 FSI 中工作)?

open System
open System.Net

let fetchUrlAsync url = 
    async {
        Console.WriteLine(sprintf "Fetch <%s>" url)
        let req = WebRequest.Create(Uri(url)) 
        use! resp = req.AsyncGetResponse()   
        use stream = resp.GetResponseStream() 
        use reader = new IO.StreamReader(stream) 
        let html = reader.ReadToEnd() 
        Console.WriteLine(sprintf "finished downloading %s. Length = %i" url html.Length)
    }

[<EntryPoint>]
let main argv =
    ["http://bing.com"; "http://ya.ru"; "http://google.com"]
    |> List.map fetchUrlAsync
    |> Async.Sequential
    |> Async.Ignore
    |> Async.RunSynchronously


Run Code Online (Sandbox Code Playgroud)

输出:

Fetch <http://bing.com>
Fetch <http://ya.ru>
Fetch <http://google.com>
finished downloading http://google.com. Length = 50592
finished downloading http://ya.ru. Length = …
Run Code Online (Sandbox Code Playgroud)

f# asynchronous f#-interactive

4
推荐指数
1
解决办法
98
查看次数

.fsx文件是在调试或发布模式下交互式编译的吗?

使用fsi.exe启动.fsx时,代码是在调试或发布模式下以交互方式编译的吗?

因为我做了fsi.exe --debug test.fsx,它仍然打印"发布".

test.fsx:

...
    #if DEBUG
        do printf "debug"
    #else
        do printf "release"    
    #endif
...
Run Code Online (Sandbox Code Playgroud)

我错过了什么?谢谢!

f# f#-interactive

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

F#递归函数在奇怪的无限循环中

当谈到F#时,我非常环保,而且我遇到了一个处理递归函数的小问题,我希望能帮助我理解.

我有一个函数应该吐出下一个偶数:

let rec nextEven(x) =
    let y = x + 1
    if y % 2 = 0 then y
    else nextEven y


// This never returns..
nextEven 3;;
Run Code Online (Sandbox Code Playgroud)

我使用'rec'关键字使它会递归,但是当我使用它时,它会因某种原因在无限循环中运行.如果我像这样重写函数:

let nextEven(x) =
    let y = x + 1
    if y % 2 = 0 then y
    else nextEven y
Run Code Online (Sandbox Code Playgroud)

然后一切正常(没有rec关键字).出于某种原因,我虽然我需要'rec',因为函数是递归的(为什么我不这样做?)为什么函数的第一个版本会永远运行?

编辑
原来这是一个总的noob错误.我已经创建了该函数的多个定义,如注释+答案中所述.

f# f#-interactive

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