I am reading the book `The Haskell Road to Math, Logic, ...". One of the exercises in the first chapter asks for a function that maps a string "abcd" to "abbcccdddd" and "bang!" to "baannngggg!!!!!". Since such a function f fixes the empty word, and maps wa to f(w)a^(length(w)+1) for any word w and any letter a, I came up with the following solution:
-- Map "abcd" to "abbcccdddd" and "bang!" to "baannngggg!!!!!". blowup :: String -> String blowup [] = [] blowup x = blowup (allButLast x) ++ lastToTheLength x -- Map "abcd" to "abc". allButLast :: String -> String allButLast [] = [] allButLast [x] = [] allButLast (x : xs) = x : allButLast xs -- Map "abcd" to d^4 = "dddd". lastToTheLength :: String -> String lastToTheLength [] = [] lastToTheLength [x] = [x] lastToTheLength (_ : xs) = lastToTheLength xs ++ [last xs] One question I have is whether I can eliminate points in the above definition of blowup, and write something like blowup = (++) . (blowup . allButLast, lastToTheLength) thinking of (++) as a function String x String -> String. Also, I can't figure out whether it is possible to get a shorter solution using fold. I have tried Hlint on my file, but it gave no suggestions. I am sure there are better ways, and would like some pointers and any general suggestions for improvement. Thanks and regards, Raghavendra. -- N. Raghavendra <ra...@mri.ernet.in> | http://www.retrotexts.net/ Harish-Chandra Research Institute | http://www.mri.ernet.in/ See message headers for contact and OpenPGP information. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe