In: Computer Science
HASKELL PROGRAMMING
Use the functions concatenate AND concatList to write the functions below:
1) WRITE the function: concatStringsUsingDelimiter (can use fold instruction and a lambda expression to define)
Sample run = *Main> concatStringsUsingDelimiter '#' ["The", "quick" , "boy" , "ran."]
"The#quick#boy#ran.#"
2) WRITE the function: unconcatStringWithDelimiter (inverse of function above. can use haskell's let construct and/or where construct)
Sample run = *Main> unconcatStringWithDelimiter '#'
"The#quick#boy#ran.#"
["The" , "quick" , "boy" , "ran."]
SIDE NOTE: Please write comments describing each function and a type definition for each function preceding the function definition
-- function signature taking delimitor and array and returns string sequence
concatStringsUsingDelimiter :: [a] -> [[a]] -> [a]
-- return empty string if there is input array is empty
concatStringsUsingDelimiter s [] = []
-- returns single element if the input is also a single
concatStringsUsingDelimiter s [x] = x
concatStringsUsingDelimiter s (x:xs) = x ++ s ++ (concatStringsUsingDelimiter s xs)
-- function signature taking delimitor and array and returns Array sequence
unconcatStringWithDelimiter :: Char -> String -> [String]
-- returns empty array if the input string is empty
unconcatStringWithDelimiter _ "" = [];
-- here is the mail logic which we remove the delimitor elements from the given the string makes an array out of it
unconcatStringWithDelimiter delimiterChar inputString = foldr f [""] inputString
where f :: Char -> [String] -> [String]
f currentChar allStrings@(partialString:handledStrings)
| currentChar == delimiterChar = "":allStrings
| otherwise = (currentChar:partialString):handledStrings