Home > Software engineering >  I need to return the string instead of True in Haskell
I need to return the string instead of True in Haskell

Time:02-11

I have a list of sentences and need to print the quotes with the 'theWord' include inside. it returns True but I need to see the sentences.

myList = ["hello world","good boy","good night"]

theWord = "good"

theQuote = filter (==True) (map (isInfixOf theWord) myList)

output:

*Main> theQuote 

[True,True]

CodePudding user response:

The filter takes a predicate to check what items to retain, you thus can use isInfixOf theWord as predicate:

theQuote :: [String]
theQuote = filter (isInfixOf theWord) myList

CodePudding user response:

isInfix theWord is the predicate to pass to filter, which should iterate over the list itself.

theQuote = filter (isInfixOf theWord) myList
  • Related