简体   繁体   中英

Can I create an Haskell function that put some strings in a tuple or in a list?

For example I have a string in input: "aaa bbb ccc dddd" and my function give me

["aaa", "bbb", "ccc", "dddd"]

or

("aaa", "bbb", "ccc", "dddd")

Is possible?

We can just reason about the types and then look for the function in Hoogle.

For example, you need a function, which accepts a String and returns a list of String s, eg String -> [String] . Let's look for it:

https://www.haskell.org/hoogle/?hoogle=String+-%3E+%5BString%5D

The first one is lines with a description:

breaks a string up into a list of strings at newline characters. The resulting strings do not contain newlines.

Hm, but we need to break at spaces, not only at newline characters. Maybe there's something else?

What about words ?

breaks a string up into a list of words, which were delimited by white space.

Yea, the right, one.

Thus, the answer is:

You need the words function:

words "aaa bbb ccc dddd" returns ["aaa","bbb","ccc","dddd"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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