无法理解为什么可能在 Haskell 中键入签名
Troubles understanding why maybe type signature in Haskell
您好,我无法理解我的函数所需的类型签名。
-- findPassword :: Map.Map Hash Passwd -> Int -> Hash -> Maybe Passwd
findPassword rTable width hashVal = do
let usefulHashes = take (width+1) (iterate (pwHash.pwReduce) hashVal)
let hashesInMap = [i | i <- usefulHashes, Map.member i rTable]
let goodPass = [ rTable Map.! j | j <- hashesInMap]
let findPass = listToMaybe [ helper k hashVal width | k <- goodPass, (helper k hashVal width) /= "" ]
return findPass
where
helper :: Passwd -> Hash -> Int -> Passwd
helper passW hashVal width
| (pwHash passW) == hashVal = passW
| width == 0 = ""
| otherwise = helper (pwReduce (pwHash passW)) hashVal (width-1)
在这个函数中,我使用一个 table,它是哈希值 (Int32) 的映射作为密码 (Strings) 的键,并尝试在 table 中找到给定的哈希。找到我要查找的密码后,我使用 listToMaybe 和 return 值作为可能的密码。但是,当我 运行 收到此错误时:
* Couldn't match type `Maybe Passwd' with `[Char]'
Expected type: Maybe Passwd
Actual type: Maybe (Maybe Passwd)
* In a stmt of a 'do' block: return result
In the expression:
do let usefulHashes
= take (width + 1) (iterate (pwHash . pwReduce) hashVal)
let hashesInMap = ...
let goodPass = ...
let findPass = ...
....
In an equation for `findPassword':
findPassword rTable width hashVal
= do let usefulHashes = ...
let hashesInMap = ...
let goodPass = ...
....
where
turntoMaybe :: [Passwd] -> Maybe Passwd
turntoMaybe list = listToMaybe list
helper :: Passwd -> Hash -> Int -> Passwd
helper passW hashVal width
| (pwHash passW) == hashVal = passW
| width == 0 = ""
| otherwise = helper (pwReduce (pwHash passW)) hashVal (width - 1)
|
78 | return result
所以我的问题是为什么它需要一个 Maybe (Maybe Passwd) 值?如果我将类型签名切换为 Maybe (Maybe Passwd) 它可以工作,但输出预期是双 Just Just "Passwd"。如果我删除类型签名,它就可以正常工作。这只是项目其余部分的一小部分,所以如果需要任何说明,请告诉我。
Haskell 不是 Java。您不应该以 return
结束所有内容。要使其正常工作,只需将 return findPass
更改为 findPass
。您还应该考虑删除多余的 do
块,只使用 where
而不是所有 let
。
您好,我无法理解我的函数所需的类型签名。
-- findPassword :: Map.Map Hash Passwd -> Int -> Hash -> Maybe Passwd
findPassword rTable width hashVal = do
let usefulHashes = take (width+1) (iterate (pwHash.pwReduce) hashVal)
let hashesInMap = [i | i <- usefulHashes, Map.member i rTable]
let goodPass = [ rTable Map.! j | j <- hashesInMap]
let findPass = listToMaybe [ helper k hashVal width | k <- goodPass, (helper k hashVal width) /= "" ]
return findPass
where
helper :: Passwd -> Hash -> Int -> Passwd
helper passW hashVal width
| (pwHash passW) == hashVal = passW
| width == 0 = ""
| otherwise = helper (pwReduce (pwHash passW)) hashVal (width-1)
在这个函数中,我使用一个 table,它是哈希值 (Int32) 的映射作为密码 (Strings) 的键,并尝试在 table 中找到给定的哈希。找到我要查找的密码后,我使用 listToMaybe 和 return 值作为可能的密码。但是,当我 运行 收到此错误时:
* Couldn't match type `Maybe Passwd' with `[Char]'
Expected type: Maybe Passwd
Actual type: Maybe (Maybe Passwd)
* In a stmt of a 'do' block: return result
In the expression:
do let usefulHashes
= take (width + 1) (iterate (pwHash . pwReduce) hashVal)
let hashesInMap = ...
let goodPass = ...
let findPass = ...
....
In an equation for `findPassword':
findPassword rTable width hashVal
= do let usefulHashes = ...
let hashesInMap = ...
let goodPass = ...
....
where
turntoMaybe :: [Passwd] -> Maybe Passwd
turntoMaybe list = listToMaybe list
helper :: Passwd -> Hash -> Int -> Passwd
helper passW hashVal width
| (pwHash passW) == hashVal = passW
| width == 0 = ""
| otherwise = helper (pwReduce (pwHash passW)) hashVal (width - 1)
|
78 | return result
所以我的问题是为什么它需要一个 Maybe (Maybe Passwd) 值?如果我将类型签名切换为 Maybe (Maybe Passwd) 它可以工作,但输出预期是双 Just Just "Passwd"。如果我删除类型签名,它就可以正常工作。这只是项目其余部分的一小部分,所以如果需要任何说明,请告诉我。
Haskell 不是 Java。您不应该以 return
结束所有内容。要使其正常工作,只需将 return findPass
更改为 findPass
。您还应该考虑删除多余的 do
块,只使用 where
而不是所有 let
。