简体   繁体   English

使用单词的字符串列表的Haskell字符串

[英]Haskell String to List of Strings using Words

I'm rather new to Haskell, and I'm currently using LearnYouAHaskell. 我对Haskell很新,我现在正在使用LearnYouAHaskell。 I am trying to take a string separated by white space, and break it into a list of smaller word strings. 我试图将一个由空格分隔的字符串,并将其分解为一个较小的字符串列表。 My current program: 我目前的计划:

main = do 
    putStrLn "Insert a string to convert: "
    -- Input string
    line <- getLine
    words line;

But in this case, it tells me I'm having an IO error. 但在这种情况下,它告诉我我有一个IO错误。 TO my understanding, getLine is an action, and so since this is impure, I have to bind it to "line". 根据我的理解,getLine是一个动作,因此,由于这是不纯的,我必须将它绑定到“行”。 Line is an accurate representation of getLine, which is an IO String. Line是getLine的精确表示,它是一个IO String。

However, shouldn't line be a string? 但是,不应该成为一个字符串? When I try to use words on line, it tells me "Couldn't match expected type "IO a0" with actual type [String] 当我尝试在线使用单词时,它告诉我“无法匹配预期类型”IO a0“与实际类型[String]

As if line isn't a string. 好像line不是字符串。 Furthermore, can I use :t line in the program itself when I make it to see if it's actual of the right type or not? 此外,我可以在程序中使用:t行,当我查看它是否是正确类型的实际?

I apologize for the novice question, but I'm a bit stuck. 我为新手问题道歉,但我有点卡住了。

EDIT: 编辑:

I did something similar in GHCI, and it tells me that my type is in fact a normal string.. I don't get it. 我在GHCI做了类似的事情,它告诉我我的类型实际上是一个普通的字符串..我不明白。

Prelude> line <- getLine
"Hello fellows"
Prelude> :t line
line :: String
Prelude> words line
["Hello","fellows"]

Why doesn't that work? 为什么不起作用?

In haskell if you want to return a value, you have to say so: 在haskell中,如果你想返回一个值,你必须这样说:

main = do 
    putStrLn "Insert a string to convert: "
    -- Input string
    line <- getLine
    return (words line)

words line isn't an IO action, it's a list of strings, so it can't be a statement in a do block. words line不是IO动作,它是一个字符串列表,因此它不能是do块中的语句。

return :: Monad m => a -> ma and in this case we can specialise it to the type a -> IO a and then to [String] -> IO [String] . return :: Monad m => a -> ma ,在这种情况下,我们可以将它专门化为类型a -> IO a然后再调整到[String] -> IO [String] Each of the statements in your do block must be IO statements. do块中的每个语句do必须是IO语句。


Taking it further: 进一步说:

If you want to compile your program, you should have main :: IO() , which means you shouldn't return your list. 如果你想编译你的程序,你应该有main :: IO() ,这意味着你不应该返回你的列表。

If, for example, you wanted to process those strings into a single string then output that, you could do 如果,例如,你想process这些字符串连接成一个字符串,然后输出,你可以做

process :: [String] -> String
process xss = "I don't know, some answer"

main = do 
    putStrLn "Insert a string to convert: "
    -- Input string
    line <- getLine
    putStrLn (process (words line))

although I'd personally write that last line as putStrLn $ process.words $ line . 虽然我亲自把最后一行写成putStrLn $ process.words $ line


Your interaction in GHCi 您在GHCi中的互动

Prelude> line <- getLine
"Hello fellows"
Prelude> :t line
line :: String
Prelude> words line
["Hello","fellows"]

is using the fact that GHCi isn't actually just running in the IO monad. 使用的事实是GHCi实际上并不只是在IO monad中运行。 In GHCi, if your input is a valid line in a do block, it'll get run, but if it's pure code it'll get evaluated and printed. 在GHCi中,如果你的输入是do块中的有效行,它将被运行,但如果它是纯代码,它将被评估和打印。 (An interactive interpreter like this is often called a REPL for Read-Eval-Print-Loop.) (像这样的交互式解释器通常称为Read-Eval-Print-Loop的REPL。)

Well, the question is what do you want to do with words line ? 好吧,问题是你想用words line做什么?

Having words line as a line inside a do block is doing nothing, but to get it to work you have to use return to wrap it up in the IO monad: 在do块中将words line作为一行无效,但要使其工作,您必须使用return将其包装在IO monad中:

main = do 
    putStrLn "Insert a string to convert: "
    -- Input string
    line <- getLine
    return (words line);

Anyway, perhaps you want to print it instead? 无论如何,也许你想要打印它?

main = do 
    putStrLn "Insert a string to convert: "
    -- Input string
    line <- getLine
    print (words line);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM