在case语句中解析错误

imc*_*imc 4 haskell type-conversion pattern-matching parse-error maybe

我想在Haskell中将Maybe Int转换为Int,如下所示:

convert :: Maybe Int -> Int
convert mx = case mx of 
               Just x -> x
              Nothing -> error "error message"
Run Code Online (Sandbox Code Playgroud)

当我编译它时,Haskell告诉我:parse error on input 'Nothing'.

我需要这个,因为我想从Data.List模块中使用elem.Index函数获取列表中元素的索引,然后在take函数上使用此索引.我的问题是elemIndex返回a Maybe Int,但take需要一个Int.

lef*_*out 7

这是一个空白问题.这些case子句需要缩进到同一级别.

convert :: Maybe Int -> Int
convert mx = case mx of 
               Just x -> x
               Nothing -> error "error message"
Run Code Online (Sandbox Code Playgroud)

请记住只使用空格,没有标签.

  • 也就是说,OP似乎想要的是[`fromJust`](http://hackage.haskell.org/package/base-4.8.0.0/docs/Data-Maybe.html#v:fromJust). (4认同)
  • @bereal:mhhhh ......我不推荐`fromJust`.请参阅上面对该问题的评论.(但它仍然比`convert`更好.) (4认同)