Haskell : finding substring with char index

import Data.List
wordFinderHelper reverseword content = foldl step (0,[],1,””) content
where step (x,ys,i,matcher) c | c:matcher == reverseword = ((x+1),i-length reverseword:ys,i+1,””)
| (c:matcher) `isSuffixOf` reverseword = (x,ys,i+1,c:matcher)
| otherwise = (x,ys,i+1,””)

wordFinder word content = (\(x,y,_,_)-> (x,reverse y))$wordFinderHelper (reverse word) content

–sample run
wordFinder “hello” “hello again hellohello DJ helloi”
(4,[0,12,17,30])

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