Sunday 5 June 2016

Haskell: Tokenize a string based on a character


Sample.hs
{-# LANGUAGE BangPatterns #-}
type Token = Char
type Input = String

getTokens :: Input -> Token -> [Input]
getTokens [] token = []
getTokens input token = getTokens' input token []

getTokens' :: Input -> Token -> [Token] -> [Input]
getTokens' [] token result = [result]
getTokens' (x:xs) token !result
    | (x == token) = result : getTokens' xs token []
    | otherwise = getTokens' xs token ( result ++ [x] )

Prelude> :load Sample.hs
[1 of 1] Compiling Main             ( Sample.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> getTokens "Hello Ptr How are you" ' '
["Hello","Ptr","How","are","you"]
*Main> 
*Main> getTokens "Hello Ptr How are you" 'P'
["Hello ","tr How are you"]
*Main> 
*Main> getTokens "Hello Ptr How are you" 'H'
["","ello Ptr ","ow are you"]
*Main> 
*Main> getTokens "1,2,3,4,5,6,7,8" ','
["1","2","3","4","5","6","7","8"]


Previous                                                 Next                                                 Home

No comments:

Post a Comment