Haskell: Finding no of occurrence of words with string index

wordFilnderHelper word content = foldl step (0,[],0) $ words content
where step (x,y,z) w | word == w = ((x+1),z:y,z+1)
| otherwise = (x,y,z+1)

wordFinder word content = (\(x,y,z)->(x,reverse y))$wordFilnderHelper word content

–sample run
wordFinder “hello” “hello this is tough and hello agin hello this is tougher helloi hello”
(4,[0,5,7,12])

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s