Function F中的类型不匹配错误#
下面的脚本应该计算数字的第一个素数因子。然而,它在第10行抛出一个错误,即Function F中的类型不匹配错误#,function,f#,functional-programming,f#-interactive,f#-3.0,Function,F#,Functional Programming,F# Interactive,F# 3.0,下面的脚本应该计算数字的第一个素数因子。然而,它在第10行抛出一个错误,即 ~vs7F27.fsx(10,28): error FS0001: Type mismatch. Expecting a unit list but given a int64 list The type 'unit' does not match the type 'int64' 我的代码如下。为什么它想要一个单元作为这里的类型?如何更改代码以允许使用int64 let makeList x
~vs7F27.fsx(10,28): error FS0001: Type mismatch. Expecting a
unit list
but given a
int64 list
The type 'unit' does not match the type 'int64'
我的代码如下。为什么它想要一个单元作为这里的类型?如何更改代码以允许使用int64
let makeList x = [2L..(x-1L)]
let divides x y = x%y = 0L
let isprime n =
let rec check i =
i > n/2L || (n % i <> 0L && check (i + 1L))
check 2L
let findFirstPrimeFactor x =
let rec find y list =
if divides y (list |> List.head) && list |> List.head |> isprime
then List.head(list)
if list |> List.length <> 1 then 1L
else find y (list |> List.tail)
find x (makeList x)
findFirstPrimeFactor 7L
让makeList x=[2L..(x-1L)]
让x除以y=x%y=0L
让我来=
让rec检查我=
i>n/2L | |(n%i 0L&检查(i+1L))
检查2L
让FindFirstTime因子x=
让rec查找y列表=
如果除以y(list |>list.head)和&list |>list.head |>isprime
然后列表。头(列表)
如果list |>list.length 1,则为1L
else查找y(list |>list.tail)
查找x(生成列表x)
FindFirstTime因子7L
您的代码缩进有误导性。应该更像
let findFirstPrimeFactor x =
let rec find y list =
if divides y (list |> List.head) && list |> List.head |> isprime
then List.head(list)
if list |> List.length <> 1 then 1L
else find y (list |> List.tail)
find x (makeList x)
let findFirstPrimeFactor x =
let rec find y list =
if divides y (list |> List.head) && list |> List.head |> isprime
then List.head(list)
elif list |> List.length <> 1 then 1L
else find y (list |> List.tail)
find x (makeList x)