使用elemIndex将MaybeInt转换为Int

时间:2018-09-21 16:46:33

标签: haskell types maybe

我有一个表(字符串列表),并有一个“选择”方法来删除某些行。 Select获取列名和一个值,并删除该列中没有该值的所有行。我使用了elemIndex,并收到类型不匹配错误(可能是Int-> Int)。

  type Field = String
  type Row   = [Field]
  type Table = [Row]

  select :: Field -> Field -> Table -> Table
  select column value table@(header:rows)
      = header : (filter rowHasValue rows)
      where columnIndex     = elemIndex column header
            rowHasValue row = (row !! columnIndex) == value;

我尝试创建另一个函数,但是在哪里得到解析错误

maybeValue :: Field -> Field -> Int
maybeValue column header = case maybeValue of
                                Just n -> n
                                Nothing ->
    where maybeValue = elemIndex column header;

确切的错误消息:

Main.hs:85:39: error:
    * Couldn't match expected type `Int' with actual type `Maybe Int'
    * In the second argument of `(!!)', namely `columnIndex'
      In the first argument of `(==)', namely `(row !! columnIndex)'
      In the expression: (row !! columnIndex) == value
   |
85 |             rowHasValue row = (row !! columnIndex) == value

0 个答案:

没有答案