我是Haskell的新手,我正在尝试将一个简单的密码程序作为我的第一个程序。我遇到了一个问题,我不太确定如何修复它。我不知道如何为密码制作第二部分。
main = do
putStrLn "Hello, Who are you?"
name <- getLine --User Input
putStrLn ("Hey " ++ name ++ ", What's the password?")
pass <- getLine
if pass == "12345"
then do
putStrLn ("Welcome")
pw -- Go to password Prompt
else do
putStrLn "That is wrong!"
main --Sent back to beginning
pw = do
putStrLn "What Password do you need?"
我不知道如何切换到询问需要哪个密码,我很确定我知道如何列出它们。目标是询问用户哪个密码提供诸如yahoo之类的网站列表,然后用户选择一个并被告知密码。提前致谢:D
答案 0 :(得分:2)
如果我理解正确,你正在尝试创建这样的东西:
What password do you need?
用户输入 Yahoo
OK, the password for the website "Yahoo" is "asdf"
为此,您需要一组包含相关密码的网站。您可以将网站和密码表示为String
。关联列表的最佳集合(这是您需要的)是Map
。因此,在模块的顶部,导入Data.Map.Strict
:
import qualified Data.Map.Strict as Map
现在您可以访问记录的所有类型和功能here。现在,您可以使用Map.fromList :: [(String,String)] -> Map.Map String String
将[(String,String)]
(String
s对的列表)转换为Map.Map String String
(从String
到其他String
的映射websites :: Map.Map String String
websites = Map.fromList
[("Yahoo","asdf")
,("Google","meow")
-- You can add more here if you need to
]
S):
Map.lookup :: String -> Map.Map String String -> Maybe String
现在,您可以使用Map.lookup "Yahoo" websites ==> Just "asdf"
查找网站并获取其相关密码:
Just "asdf"
请注意,它返回Nothing
而不是Yahoo
,因为它能够在websites
中找到-- Get the website name and put it in websiteName
putStrLn "What password do you need?"
websiteName <- getLine
-- Check our Map for the website name the user gave us
case Map.lookup websiteName websites of
-- If we found the password, print it out with a nice message
Just thePassword -> putStrLn $ "OK, your password is: " ++ thePassword
-- If that website was not in our Map, print an error message
Nothing -> putStrLn "Website not found :("
。现在我们只需要一点IO胶水来获取用户的输入并打印出结果,例如:
$('.remove').click(function()...)