小编rmu*_*unn的帖子

为什么foldBack没有执行折叠相同的副作用?

我是通过回答工作的不同的实例之间List.fold和List.foldBack试图让我周围的区别头部foldfoldBack.我现在理解应用程序顺序的差异,但是我不明白的副作用存在差异.

我用过List.foldList.foldBack测试.我的累加器函数基本上等同于::,因此累加顺序很重要.我使用的累加器函数如下:

let f acc x =
  // printfn "Folding %A into %A" x acc  // Side-effect!
  x :: acc

let f2 x acc =
  // printfn "Folding %A into %A" x acc  // Side-effect!
  x :: acc
Run Code Online (Sandbox Code Playgroud)

我从F#参考中了解到:

List.fold f [] [1; 2; 3; 4; 5] = (f (f (f (f (f [] 1) 2) 3) 4) 5)
Run Code Online (Sandbox Code Playgroud)

和:

List.foldBack f2 [] [1; 2; 3; 4; 5] …
Run Code Online (Sandbox Code Playgroud)

f# fold

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

F# pipe function execution flow

I am a F# newbie here, the following code is to get all the lines from a csv file which rating > 9.0 then output to a new file.

But I have a hard time figuring out how many for loop does it takes to get the job done, is it all the steps are done in one for loop or 5 for loops as I highlight below?

If it need 5 for loops to get the job done, it …

f#

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

简单的rec函数中的F#和内存泄漏

我有一个侦听连接客户端的简单TCP服务器,它看起来很简单

let Listener (ip:IPAddress) (port:int32) =
    async {    
        let listener = TcpListener(ip, port)
        listener.Start()   
        _logger.Info(sprintf "Server binded to IP: %A - Port: %i" ip port)

        let rec listenerPending (listener : TcpListener) = 
            async {
                if not (listener.Pending()) then return! listenerPending listener // MEMMORY LEAK SOURCE
                printfn "Test"
             }

        listenerPending listener |> Async.Start
    }
Run Code Online (Sandbox Code Playgroud)

好吧,它看起来很简单,但是我有一个内存泄漏问题:当它等待连接时,它会像糖果一样吞噬RAM。

我想它与递归函数有关,但是不知道该如何稳定它。

f#

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

列表的重复项目

如果我想复制列表中的元素,我将如何进行并执行此操作?我在考虑以下几点:

let List = [(1,2);(3,4)]
let v = List.map(fun (x,y) -> List.replicate 2 (x,y)) List
Run Code Online (Sandbox Code Playgroud)

我希望结果是[(1,2); (1,2); (3,4); (3,4)].任何建议将不胜感激.如果可能的话,我想只使用更高的顺序.

f#

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

F#拒绝基于多个约束确定类型

我已尽一切努力使编译器能够监听。但是,它拒绝理解。我正在尝试比较每个元素的颜色值,如果它们相同,则返回true,否则返回false。

我放置了想要的约束,但仍然无法确定类型。

let all_same_color cs =
  let mutable d=true
  let (col:Color) = card_color (cs.Head:Card)
  for i in cs do
    let col=card_color i
    if not (col = col) then
      printfn "Black"
      d<-false
    else
      d<-d
    printfn "Val %b" d
  d
Run Code Online (Sandbox Code Playgroud)

我希望如果颜色匹配则返回true,否则返回false。

它在这一行不断出错:

let (col:Color) = card_color (cs.Head:Card)

 Lookup on object of indeterminate type based on information prior to this program point. A type annotation may be needed prior to this programpoint to constrain the type of the object. This may allow the …
Run Code Online (Sandbox Code Playgroud)

f# types list

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

标签 统计

f# ×5

fold ×1

list ×1

types ×1