Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to verify if some items are in a list?

Tags:

haskell

I'm trying to mess about trying the haskell equivalent of the 'Scala One Liners' thing that has recently popped up on Reddit/Hacker News.

Here's what I've got so far (people could probably do them a lot better than me but these are my beginner level attempts)

https://gist.github.com/1005383

The one I'm stuck on is verifying if items are in a list. Basically the Scala example is this

val wordlist = List("scala", "akka", "play framework", "sbt", "typesafe")
val tweet = "This is an example tweet talking about scala and sbt."
(words.foldLeft(false)( _ || tweet.contains(_) ))

I'm a bit stumped how to do this in Haskell. I know you can do:

any (=="haskell") $ words "haskell is great!"

To verify if one of the words is present, but the Scala example asks if any of the words in the wordlist are present in the test string.

I can't seem to find a contains function or something similar to that. I know you could probably write a function to do it but that defeats the point of doing this task in one line.

Any help would be appreciated.

like image 487
djhworld Avatar asked Jun 03 '11 07:06

djhworld


2 Answers

You can use the elem function from the Prelude which checks if an item is in a list. It is commonly used in infix form:

Prelude> "foo" `elem` ["foo", "bar", "baz"]
True

You can then use it in an operator section just like you did with ==:

Prelude> let wordList = ["scala", "akka", "play framework", "sbt", "types"]
Prelude> let tweet = "This is an example tweet talking about scala and sbt."
Prelude> any (`elem` wordList) $ words tweet
True

When you find yourself needing a function, but you don't know the name, try using Hoogle to search for a function by type.

Here you wanted something that checks if a thing of any type is in a list of things of the same type, i.e. something of a type like a -> [a] -> Bool (you also need an Eq constraint, but let's say you didn't know that). Typing this type signature into Hoogle gives you elem as the top result.

like image 135
hammar Avatar answered Oct 11 '22 22:10

hammar


How about using Data.List.intersect?

import Data.List.intersect

not $ null $ intersect (words tweet) wordList
like image 6
nimrodm Avatar answered Oct 12 '22 00:10

nimrodm