【问题标题】:This expression was expected to have type unit此表达式应具有单位类型
【发布时间】:2015-09-22 17:22:05
【问题描述】:

我有以下代码。在这一行

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

我有 2 个错误。 首先在0

 This expression was expected to have type
    unit    
but here has type
    int

然后在i

This expression was expected to have type
    unit    
but here has type
    int

* 我也看过this 并尝试忽略,但它不起作用

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

【问题讨论】:

  • 它应该是读者的难题吗? :) 如果您不确定表达式的边界,请尝试使用括号。
  • 你不是在 2 小时前问过这个吗? stackoverflow.com/q/32720710/126014

标签: f# f#-interactive unit-type


【解决方案1】:

您不需要ignore - 如果您使用赋值,它会返回unit,因此您没有任何需要忽略的返回值:

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

也就是说,这不是很实用的方法。因此,查看一些基本的 F# 书籍或观看一些讲座可能会帮助您以更 F# 的风格开始使用该语言。

【讨论】:

    【解决方案2】:

    你只需要括号来让编译器清楚你的意图:

    if min <= 0 then (min <- List.nth list i) |> ignore
    

    在 F# 中没有 else 的 if 是以下的简写:

    if condition then doSomething else ()
    

    这意味着doSomething 块内的任何结果必须是unit 类型。由于 F# 中的赋值是一个表达式,因此您的代码返回 min,一个 int 值。这解释了您的第一个错误。

    发生上述情况是因为,没有括号,管道运算符使用最后一个参数 os List.nthi 作为 ignore 的参数

    【讨论】:

      【解决方案3】:

      第一个问题是list |&gt; List.rev |&gt; List.head 导致编译器将list 推断为unit 类型。如果您删除该行(因为无论如何它毫无意义,F# 列表是不可变的,因此您正在计算一个未使用的值),list 被正确推断为具有类型 int list 这使得第一个错误消失(如果我们也使用 @ 987654326@ 而不是 list.Head 以使类型推断快乐)。

      然后,if min&lt;=0 then min &lt;- List.nth list i |&gt;ignore 这一行出现第二个错误,这是有道理的,因为对可变变量的赋值不应该给|&gt; 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
      

      不过,它看起来并不像 F#-ish。这个怎么样(包括你想实现的疯狂猜测):

      let printMinMax (a, b, c, d, e) =
      
          let minPositive sortedList = 
              sortedList |> List.fold (fun m e -> if m <= 0 then e else m) sortedList.Head
      
          let max sortedList = sortedList |> List.last
      
          let sortedList = [ a; b; c; d; e ] |> List.sort
      
          printfn "min %d, max %d" (minPositive sortedList) (max sortedList)
      
      let t1 = 14, 6, 18, 76, 76
      printMinMax t1
      
      let t2 = -1, -5, 5, 16, 12
      printMinMax t2
      

      这可以进一步改进,但我担心与原始的联系变得不那么明显(并且它预计至少存在一个正值):

      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
      

      【讨论】:

        猜你喜欢
        • 2021-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-26
        • 1970-01-01
        • 2015-02-28
        相关资源
        最近更新 更多