1

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"]
2
  • 9
    Remove fromList and you are done. The brackets already form a list, there's no need to do anything else. Commented Oct 12, 2017 at 10:00
  • 2
    Well if you want a list of strings, I do not see why you need a fromList. You already constructed a list. Commented Oct 12, 2017 at 10:08

1 Answer 1

1

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). If y is not in x the output should be

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.