F#函数运行时没有被调用

时间:2016-05-23 02:39:00

标签: .net f#

此代码

Public Class Form1
   Dim counter As Integer
   Dim num() As Integer
Private Sub btnMean_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMean.Click
   getNumbers()
   lstOutput.Items.Add("The mean is: " & FindMean(num, counter))
End Sub

Sub getNumbers()
   Dim sr As IO.StreamReader
   sr = IO.File.OpenText("digits.txt")
   Do While sr.Peek <> -1
      counter += 1
   Loop
   sr.Close()
   sr = IO.File.OpenText("digits.txt")
   Do While sr.Peek <> -1
     For i As Integer = 0 To counter
       num(i) = CInt(sr.ReadLine)
     Next
   Loop
   sr.Close()
End Sub
Function FindMean(ByRef num() As Integer, ByRef counter As Integer) As Double
   Dim total As Integer = 0
   For k As Integer = 0 To num.GetUpperBound(0)
     total += num(k)
   Next
   Return (total / counter)
End Function
End Class

具有以下输出:

open System.Threading

let duration = 1000

module SequentialExample =
    let private someTask item =
        printfn "oh god why"
        Thread.Sleep(duration)
        item + " was computed"

    let private items = [
        "foo"
        "bar"
        "baz"
    ]

    let getComputedItems = 
        printfn "heh"
        [for item in items -> someTask item]
        |> Array.ofList

module ParallelExample =
    let private someTask item =
        printfn "that's ok"
        Thread.Sleep(duration)
        item + " was computed"

    let private items = [
        "foo"
        "bar"
        "baz"
    ]

    let getComputedItems = 
        Async.Parallel [for item in items -> async { return someTask item }]
        |> Async.RunSynchronously

[<EntryPoint>]
let main args =
    ParallelExample.getComputedItems |> ignore
    0

如果我正在调用heh oh god why oh god why oh god why that's ok that's ok that's ok 模块,为什么F#在ParallelExample模块中运行代码?

我做错了什么?

1 个答案:

答案 0 :(得分:7)

John Palmer在评论中说 let getComputedItems = ... 实际上是一个值,而不是一个函数,因为函数必须带参数

要使它成为一个函数,必须声明它 let getComputedItems () = ...