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
F# 此表达式应具有unit类型_F#_F# Interactive_Unit Type - Fatal编程技术网

F# 此表达式应具有unit类型

F# 此表达式应具有unit类型,f#,f#-interactive,unit-type,F#,F# Interactive,Unit Type,我有以下代码。在这一行 if min<=0 then min <- List.nth list i |>ignore 然后在i中 This expression was expected to have type unit but here has type int * 我也看到并尝试忽略,但它不起作用 let replace touple2= let first (a,_,_,_,_)=a let second (_,b,

我有以下代码。在这一行

 if min<=0  then     min <- List.nth list i |>ignore
然后在
i

This expression was expected to have type
    unit    
but here has type
    int
* 我也看到并尝试忽略,但它不起作用

let replace touple2=
   let first  (a,_,_,_,_)=a
   let second (_,b,_,_,_)=b
   let third  (_,_,c,_,_)=c
   let forth  (_,_,_,d,_)=d
   let fifth  (_,_,_,_,e)=e
   let sortedlist list= List.sort(list)

   let GetMin list=
        list |> List.rev |> List.head
        let mutable min=list.Head
        let mutable i=1
        for i in list do     
            if min<=0  then     min <- List.nth list i |>ignore

        min 

   let GetMax list=list |> List.rev |> List.head

   let A=first  touple2
   let B=second touple2
   let C=third  touple2
   let D=forth  touple2
   let E=fifth  touple2
   let mylist=[A;B;C;D;E]
   let L=sortedlist mylist

   let m1=GetMax L
   printfn "%d"  m1

let touple3= 14,6,18,76,76
replace touple3
让我们更换touple2=
让我们先(a,,,,,,,,=a
设第二(u,b,u,u,u,u)=b
设第三(u,u,c,u,u)=c
发出(u,u,u,d,u)=d
设第五(u,u,u,u,e)=e
让sortedlist列表=列表。排序(列表)
让GetMin列表=
list |>list.rev |>list.head
让可变最小值=list.Head
设可变i=1
因为我在名单上
如果min List.rev |>List.head
设A=第一个touple2
设B=2秒
设C=2
设D=2
设E=2
让mylist=[A;B;C;D;E]
设L=sortedlist mylist
设m1=GetMax L
打印fn“%d”m1
设touple3=14,6,18,76,76
更换头套3

您只需要括号就可以让编译器明白您的意图:

if min <= 0 then (min <- List.nth list i) |> ignore
这意味着
doSomething
块中任何内容的结果必须是
单元类型的。因为F#中的赋值是一个表达式,所以代码返回的是一个int值
min
。这就解释了你的第一个错误


发生上述情况的原因是,在没有括号的情况下,管道操作符使用最后一个参数os
List.nth
i
作为
ignore
的参数。您只需要括号就可以让编译器明白您的意图:

if min <= 0 then (min <- List.nth list i) |> ignore
这意味着
doSomething
块中任何内容的结果必须是
单元类型的。因为F#中的赋值是一个表达式,所以代码返回的是一个int值
min
。这就解释了你的第一个错误


发生上述情况的原因是,在没有括号的情况下,管道操作符使用了最后一个参数os
List.nth
i
作为
ignore
的参数,如果使用赋值,则不需要
ignore
,它返回
单位
,因此您没有任何必须忽略的返回值:

if min <= 0 then min <- List.nth list I

如果min您不需要
忽略
-如果您使用赋值,它将返回
单位
,因此您没有任何必须忽略的返回值:

if min <= 0 then min <- List.nth list I

if min第一个问题是
list |>list.rev |>list.head
导致编译器推断
list
属于
单元类型。如果删除该行(因为它毫无意义,F#list是不可变的,因此您正在计算一个未使用的值),
list
被正确地推断为具有type
int list
,这会使第一个错误消失(如果我们也使用
list.head list
而不是
list.head
使类型推断愉快)

然后,如果min ignore(最小忽略)
,则第二个错误出现在此行
。因此,让我们摆脱它,修复弃用警告并添加一些格式。。。这包括:

let replace touple2 =
   let first  (a,_,_,_,_) = a
   let second (_,b,_,_,_) = b
   let third  (_,_,c,_,_) = c
   let forth  (_,_,_,d,_) = d
   let fifth  (_,_,_,_,e) = e
   let sortedlist list= List.sort(list)

   let GetMin list=
        let mutable min = List.head list
        let mutable i = 1
        for i in list do     
            if min <= 0 then min <- List.item i list

        min 

   let GetMax list = list |> List.rev |> List.head

   let A = first  touple2
   let B = second touple2
   let C = third  touple2
   let D = forth  touple2
   let E = fifth  touple2
   let mylist = [A;B;C;D;E]
   let L = sortedlist mylist

   let m1 = GetMax L
   printfn "%d" m1

let touple3 = 14,6,18,76,76
replace touple3
让我们更换touple2=
让我们先(a,,,,,,,,=a
设第二(u,b,u,u,u,u)=b
设第三(u,u,c,u,u)=c
发出(u,u,u,d,u)=d
设第五(u,u,u,u,e)=e
让sortedlist列表=列表。排序(列表)
让GetMin列表=
让可变最小值=List.head列表
设可变i=1
因为我在名单上
如果最小列表头
设A=第一个touple2
设B=2秒
设C=2
设D=2
设E=2
让mylist=[A;B;C;D;E]
设L=sortedlist mylist
设m1=GetMax L
打印fn“%d”m1
设touple3=14,6,18,76,76
更换头套3
尽管如此,它看起来并不是真正的F#ish。这个怎么样(包括胡乱猜测你想要实现什么):

让printMinMax(a,b,c,d,e)=
让MindList=
sortedList |>List.fold(乐趣ME->if m List.last
让sortedList=[a;b;c;d;e]|>List.sort
打印fn“最小%d,最大%d”(最小正分拣列表)(最大分拣列表)
设t1=14,6,18,76,76
printMinMax t1
设t2=-1,-5,5,16,12
printMinMax t2
这可以进一步改进,但我担心与原始版本的联系变得更加不明显(并且它希望至少存在一个正值):

让minMax(a,b,c,d,e)=
设l=[a;b;c;d;e]|>List.sortDescending

让positiveMin=l |>List.findBack(第一个问题是
List |>List.rev |>List.head
导致编译器推断
List
unit
类型。如果删除该行(因为它毫无意义,F#List是不可变的,因此您正在计算未使用的值),
list
被正确推断为具有类型
int list
,这会使第一个错误消失(如果我们也使用
list.head list
而不是
list.head
使类型推断愉快)

然后,如果min ignore
,则第二个错误出现在这一行
。因此,让我们消除该错误,修复弃用警告并添加一点格式…这将编译:

let replace touple2 =
   let first  (a,_,_,_,_) = a
   let second (_,b,_,_,_) = b
   let third  (_,_,c,_,_) = c
   let forth  (_,_,_,d,_) = d
   let fifth  (_,_,_,_,e) = e
   let sortedlist list= List.sort(list)

   let GetMin list=
        let mutable min = List.head list
        let mutable i = 1
        for i in list do     
            if min <= 0 then min <- List.item i list

        min 

   let GetMax list = list |> List.rev |> List.head

   let A = first  touple2
   let B = second touple2
   let C = third  touple2
   let D = forth  touple2
   let E = fifth  touple2
   let mylist = [A;B;C;D;E]
   let L = sortedlist mylist

   let m1 = GetMax L
   printfn "%d" m1

let touple3 = 14,6,18,76,76
replace touple3
让我们更换touple2=
让我们先(a,,,,,,,,=a
设第二(u,b,u,u,u,u)=b
设第三(u,u,c,u,u)=c
发出(u,u,u,d,u)=d
设第五(u,u,u,u,e)=e
让sortedlist列表=列表。排序(列表)
让GetMin列表=
让可变最小值=List.head列表
设可变i=1
因为我在名单上
如果最小列表头
设A=第一个touple2
设B=2秒
设C=2
设D=2
设E=2
让mylist=[A;B;C;D;E]
设L=sortedlist mylist
设m1=GetMax L
打印fn“%d”m1
设touple3=14,6,18,76,76
更换头套3
尽管如此,它看起来并不是真正的F#ish。这个怎么样(包括你想要实现的疯狂猜测):

让printMinMax(a,b,c,d,e)=
让MindList=
sortedList |>List.fold(乐趣ME->if m List.last
让sortedList=[a;b;c;d;e]|>List.sort
打印fn“最小%d,最大%d”(最小值)
let minMax (a, b, c, d, e) =
    let l = [ a; b; c; d; e ] |> List.sortDescending
    let positiveMin = l |> List.findBack ((<) 0)
    let max = l.Head
    positiveMin, max

let t1 = 14, 6, 18, 76, 76
let t2 = -1, -5, 5, 16, 12

let test t =
    let min, max = minMax t
    printfn "min (positive) %d, max %d" min max

test t1
test t2