Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
F# f:使用“a”;与“匹配”;“无”字块;加上;_F# - Fatal编程技术网

F# f:使用“a”;与“匹配”;“无”字块;加上;

F# f:使用“a”;与“匹配”;“无”字块;加上;,f#,F#,我正在用f#编写一个函数,这个函数可以用几个if/else块来完成,但用match/with子句读取起来要干净得多。问题是,在许多情况下,我并不关心实际匹配的变量;我在任何地方都使用“when” 问题是,是否有一种方法可以进行“匹配/仅使用”when“块而不使用实际的匹配变量 let primes (input:bigint) = let rec factors acc (input':bigint, factor:bigint) = match input' wit

我正在用f#编写一个函数,这个函数可以用几个if/else块来完成,但用match/with子句读取起来要干净得多。问题是,在许多情况下,我并不关心实际匹配的变量;我在任何地方都使用“when”

问题是,是否有一种方法可以进行“匹配/仅使用”when“块而不使用实际的匹配变量

let primes (input:bigint) = 
    let rec factors acc (input':bigint, factor:bigint) = 
        match input' with
        | _ when input' = 1I -> acc
        | _ when input = factor -> factor::acc
        | _ when input' = factor -> factor::acc
        | _ when input' % factor = 0I -> factor::factors acc (input'/factor, factor)
        | _ -> factors acc (input', factor+1I)
    in factors [] (input, 2I)

primes 1I |> List.iter (printf "%A "); printfn "..";    
primes 12I |> List.iter (printf "%A "); printfn "..";
primes 60I |> List.iter (printf "%A "); printfn "..";
primes 420I |> List.iter (printf "%A "); printfn "..";
primes 1260I |> List.iter (printf "%A "); printfn "..";
primes 13I |> List.iter (printf "%A "); printfn "..";

[编辑]有人建议,这是与相同的问题,我想是的。但如果没有这篇文章,我就不会看到卡斯滕的答案,这正是我想寻找的。现在我知道了,我想这两种选择如下

let primes (input:bigint) = 
    let rec factors acc (input':bigint, factor:bigint) = 
        match () with
        | () when input' = 1I           -> acc
        | () when input = factor        -> factor::acc
        | () when input' = factor       -> factor::acc
        | () when input' % factor = 0I  -> factor::factors acc (input'/factor, factor)
        | _ -> factors acc (input', factor+1I)
    in factors [] (input, 2I)

let primes' (input:bigint) = 
    let rec factors acc (input':bigint, factor:bigint) = 
        if   input' = 1I            then acc
        elif input = factor         then factor::acc
        elif input' = factor        then factor::acc
        elif input' % factor = 0I   then factor::factors acc (input'/factor, factor)
        else factors acc (input', factor+1I)
    in factors [] (input, 2I)

看着他们像这样并排在一起,让我觉得我应该使用elif

您可以使用活动模式,但我认为在这种情况下开销太大-如果不是,您就无法真正避开
中的
(或者至少我不知道任何方法)

有一种模式会使这一点更加明显(您不关心匹配的值,只关心
when
子句):


你可能已经看到了

这里提供的条件与不同的输入有关,因此没有太多可匹配的内容。在这种情况下,
if/else
链更加简洁。但是,您可以使用略短的
elif
关键字:

let primes (input:bigint) = 
    let rec factors acc (input':bigint, factor:bigint) =
        if input' = 1I then acc
        elif input = factor then factor::acc
        elif input' = factor then factor::acc
        elif input' % factor = 0I then factor::factors acc (input'/factor, factor)
        else factors acc (input', factor+1I)
    in factors [] (input, 2I)
当您有一个可以具有各种不同风味或形状的单一输入时,模式匹配是很有价值的,但在这种情况下,您有各种不同的输入,其中这些输入之间的关系以分层方式决定分支。

可能的重复
let primes (input:bigint) = 
    let rec factors acc (input':bigint, factor:bigint) =
        if input' = 1I then acc
        elif input = factor then factor::acc
        elif input' = factor then factor::acc
        elif input' % factor = 0I then factor::factors acc (input'/factor, factor)
        else factors acc (input', factor+1I)
    in factors [] (input, 2I)