简体   繁体   English

如何在 Haskell 中创建字符串列表?

[英]How can I create a list of strings in Haskell?

How can I create list of String in Haskell in order to use it in a function which takes the list and a word as arguments and looks for the same Char from a word in the list of String ?如何在 Haskell 中创建String列表,以便在将列表和单词作为参数并从String列表中的单词中查找相同Char的函数中使用它? Should I use Data.Map or Data.List for creating a list?我应该使用 Data.Map 还是 Data.List 来创建列表? I've tried creating it like this:我试过像这样创建它:

dictionary :: [String]
dictonary = fromList ["wit","ass","bad","shoe","cold","pie","and","or"]

Perhaps something like也许像

import Data.List
let checkIfContains :: [String] -> String -> Integer
checkIfContains x y = elemIndex y x

Then an example of running this would be:然后运行它的一个例子是:

checkIfContains ["lol", "heh"] "heh"

output: Just 1

So if you input a list of Strings x and a String y to see if y is in x , then the output is the index y in x (as here we found "heh" in index 1 of x).因此,如果您输入一个字符串列表x和一个字符串y来查看y是否在x 中,那么输出是x 中的索引y (因为我们在x 的索引 1 中找到了“heh”)。 If y is not in x the output should be如果y不在x 中,则输出应为

Nothing

Thing to note, this function finds the first occurrence of y in x, so if if you have two entries of y in x, then it'll show the index of the first occurrence.需要注意的是,这个函数在 x 中找到 y 的第一次出现,所以如果你在 x 中有两个 y 条目,那么它会显示第一次出现的索引。

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

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