I tried to write the program in Haskell that will take a string of integer numbers delimitated by comma, convert it to list of integer numbers and increment each number by 1.
For example "1,2,-5,-23,15" -> [2,3,-4,-22,16]
Below is the resulting program
import Data.List main :: IO () main = do n <- return 1 putStrLn . show . map (+1) . map toInt . splitByDelimiter delimiter $ getList n getList :: Int -> String getList n = foldr (++) [] . intersperse [delimiter] $ replicate n inputStr delimiter = ',' inputStr = "1,2,-5,-23,15" splitByDelimiter :: Char -> String -> [String] splitByDelimiter _ "" = [] splitByDelimiter delimiter list = map (takeWhile (/= delimiter) . tail) (filter (isPrefixOf [delimiter]) (tails (delimiter : list))) toInt :: String -> Int toInt = read
The most hard part for me was programming of function splitByDelimiter
that take a String and return list of Strings
"1,2,-5,-23,15" -> ["1","2","-5","-23","15"]
Thought it is working, I am not happy with the way it is written. There are a lot of parentheses, so it looks Lisp like. Also the algorithm is somewhat artificial:
Prepend delimiter to beginning of string ",1,2,-5,-23,15"
Generate list of all tails [",1,2,-5,-23,15", "1,2,-5,-23,15", ",2,-5,-23,15", .... ]
Filter and left only strings that begins with delimiter [",1,2,-5,-23,15", ",2,-5,-23,15", .... ]
Drop first delimiter and take symbols until next delimiter will be met ["1", "2", .... ]
So the questions are:
How I can improve function splitByDelimiter
?
Can I remove prepend and drop of delimiter and make direct split of string?
How I can rewrite the function so there will be less parentheses?
May be I miss something and there are already standard function with this functionality?
You can use the split() method of String class from JDK to split a String based on a delimiter e.g. splitting a comma-separated String on a comma, breaking a pipe-delimited String on a pipe, or splitting a pipe-delimited String on a pipe.
Split is used to break a delimited string into substrings. You can use either a character array or a string array to specify zero or more delimiting characters or strings. If no delimiting characters are specified, the string is split at white-space characters.
The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.
Splitting a string using strtok() in C In C, the strtok() function is used to split a string into a series of tokens based on a particular delimiter.
Doesn't Data.List.Split.splitOn do this?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With