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
? Should I use Data.Map or Data.List for creating a list?
I've tried creating it like this:
dictionary :: [String]
dictonary = fromList ["wit","ass","bad","shoe","cold","pie","and","or"]
答案 0 :(得分:1)
也许像
import Data.List
let checkIfContains :: [String] -> String -> Integer
checkIfContains x y = elemIndex y x
然后运行它的一个例子是:
checkIfContains ["lol", "heh"] "heh"
output: Just 1
因此,如果您输入字符串 x 和字符串 y 的列表,以查看 y 是否在 x ,然后输出是 x 中的索引 y (因为我们在x的索引1中找到了“heh”)。如果 y 不在 x 中,则输出应为
Nothing
需要注意的是,这个函数在x中找到第一个y,所以如果在x中有两个y项,那么它将显示第一次出现的索引。