F# 声明变量而不指定变通方法

F# 声明变量而不指定变通方法,f#,functional-programming,F#,Functional Programming,我正在用F#编写一个小型控制台应用程序 这就产生了这个 Enter the number of the option you want 0: "NDL" 1: "Deco" enter Depth in m 这意味着它打印出它假设的内容,然后直接跳转到high\u lvl\u funcs.calculate\u NDL 我想让它产生效果 Enter the number of the option you want 0: "NDL

我正在用F#编写一个小型控制台应用程序

这就产生了这个

Enter the number of the option you want
0: "NDL"
1: "Deco"
enter Depth in m 
这意味着它打印出它假设的内容,然后直接跳转到
high\u lvl\u funcs.calculate\u NDL

我想让它产生效果

  Enter the number of the option you want
    0: "NDL"
    1: "Deco"
然后,假设输入了0,然后计算
high\u lvl\u funcs.calculate\u NDL

经过一番思考和搜索,我认为这是因为F#希望在开始其余部分之前分配所有值。然后我想我需要声明一个变量而不分配它。但是人们似乎同意这在函数式编程中是不好的。从另一个问题:


因此,我的问题是,是否可以重写代码,以获得我想要的流,并避免在不分配变量的情况下声明变量?

您可以通过使
计算NDL
成为一个无参数的函数,而不是计算为
浮点的闭包来解决这个问题:

let calculate_NDL () =
然后在
匹配中将其作为函数调用,如下所示:

match opt with
| "0" -> printfn "%A" (high_lvl_funcs.calculate_NDL())
但是,我建议重构此代码,以便
calculate\u NDL
将任何必要的输入作为参数,而不是从控制台读取它们,即分别从控制台读取输入并将其传递给
calculate\u NDL

let calculate_NDL depth fn2 =
    let absDepth = lfuncs.m_to_absolute(depth)
    let table = lfuncs.read_table
    let tissue = lfuncs.create_initialise_Tissues ATM WATERVAPOUR
    lfuncs.calc_NDL absDepth fn2 table lfuncs.loading_constantpressure tissue 0.0

一般来说,编写尽可能多的不依赖I/O的代码(如从stdin读取)是一个好主意。

同样适用于
print\u opt
,它当前是一个值,但应该是一个函数
print\u opt()
。您建议在哪里执行I/O?我是否会使用可变的来进行i/O查看。
match opt with
| "0" -> printfn "%A" (high_lvl_funcs.calculate_NDL())
let calculate_NDL depth fn2 =
    let absDepth = lfuncs.m_to_absolute(depth)
    let table = lfuncs.read_table
    let tissue = lfuncs.create_initialise_Tissues ATM WATERVAPOUR
    lfuncs.calc_NDL absDepth fn2 table lfuncs.loading_constantpressure tissue 0.0