2017-10-12 84 views
1

如何在Haskell中創建String列表,以便在以列表和單詞作爲參數的函數中使用它,並從String列表中的單詞中查找相同的Char?我應該使用Data.Map還是Data.List來創建列表? 我試着像這樣創造的:如何在Haskell中創建字符串列表?

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

刪除'fromList'就完成了。括號已經形成一個列表,不需要做其他任何事情。 – chi

+2

那麼如果你想要一個*列表*的字符串,我不明白你爲什麼需要'fromList'。你已經建立了一個列表。 –

回答

1

也許類似

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

然後運行這個的一個例子是:

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

output: Just 1 

因此,如果你輸入的列表Strings x and a String y看看是否y is in x,then the output is the index y in x(因爲這裏我們在x的索引1中發現「heh」)。如果ŸX輸出應該是

Nothing 

一點需要注意,這個函數發現在x和y的第一次出現,所以如果你有在x和y的兩個條目,那麼它將顯示第一次出現的索引。