Haskell 哈斯克尔不能';t匹配预期类型‘;[(Char,b0)]&x2019;实际类型为‘;(Char,Int)和#x2019;

Haskell 哈斯克尔不能';t匹配预期类型‘;[(Char,b0)]&x2019;实际类型为‘;(Char,Int)和#x2019;,haskell,Haskell,所以我得到了这段代码,它返回了一个关于预期类型的错误 无法匹配预期的类型“[(Char,b0)]” 实际类型为“(字符,整数)” 在表达式中:newList 在列表理解的stmt中:(a,b)在do块中 newList <- zip word [0..length word] >=的类型是(Monad m)=>ma->(a->mb)->mb 由于zip返回一个列表,因此您使用的是列表monad,因此newList的类型实际上是一个(Int,Char)。然后不能将其用作后续列表理解的源 但

所以我得到了这段代码,它返回了一个关于预期类型的错误

无法匹配预期的类型“[(Char,b0)]”
实际类型为“(字符,整数)”
在表达式中:newList

在列表理解的stmt中:(a,b)在
do
块中

newList <- zip word [0..length word]
>=
的类型是
(Monad m)=>ma->(a->mb)->mb

由于
zip
返回一个列表,因此您使用的是列表monad,因此
newList
的类型实际上是一个
(Int,Char)
。然后不能将其用作后续列表理解的源

但是,您根本不需要使用
do

isPart :: Char -> Int
isPart x = let newList = zip word [0..length word]
               result = [b | (a,b) <- newList, a == x]
           in  head result

尽管更好的解决方案是将返回类型更改为
可能是Int
,而不是在
do
块中使用
fromJust

newList <- zip word [0..length word]
>=
的类型是
(Monad m)=>ma->(a->mb)->mb

由于
zip
返回一个列表,因此您使用的是列表monad,因此
newList
的类型实际上是一个
(Int,Char)
。然后不能将其用作后续列表理解的源

但是,您根本不需要使用
do

isPart :: Char -> Int
isPart x = let newList = zip word [0..length word]
               result = [b | (a,b) <- newList, a == x]
           in  head result
虽然更好的解决方案是将返回类型更改为
可能是Int
,而不是从just使用

import Data.List (elemIndex)
import Data.Maybe (fromJust)

isPart x = fromJust $ elemIndex x word