I decided to make a solution in Haskell to a problem that I found on another post here at CodeReview (link: The Beauty and the Strings The Beauty and the Strings) I would like suggestions on how to improve letterFreqs. I have tried searching for mutable arrays that could help with performance. But is it worth it? The array that is modified often is only 26 elements long. Haskell is new to me, so I find syntax of libraries such as Data.Vector, Data.Array.MArray, and Data.IO.Array difficult to grasp easily.
I decided to make a solution in Haskell to a problem that I found on another post here at CodeReview (link: The Beauty and the Strings) I would like suggestions on how to improve letterFreqs. I have tried searching for mutable arrays that could help with performance. But is it worth it? The array that is modified often is only 26 elements long. Haskell is new to me, so I find syntax of libraries such as Data.Vector, Data.Array.MArray, and Data.IO.Array difficult to grasp easily.
I decided to make a solution in Haskell to a problem that I found on another post here at CodeReview (link: The Beauty and the Strings) I would like suggestions on how to improve letterFreqs. I have tried searching for mutable arrays that could help with performance. But is it worth it? The array that is modified often is only 26 elements long. Haskell is new to me, so I find syntax of libraries such as Data.Vector, Data.Array.MArray, and Data.IO.Array difficult to grasp easily.
Beauty and the Strings in Haskell
I decided to make a solution in Haskell to a problem that I found on another post here at CodeReview (link: The Beauty and the Strings) I would like suggestions on how to improve letterFreqs. I have tried searching for mutable arrays that could help with performance. But is it worth it? The array that is modified often is only 26 elements long. Haskell is new to me, so I find syntax of libraries such as Data.Vector, Data.Array.MArray, and Data.IO.Array difficult to grasp easily.
{-# OPTIONS_GHC -F -pgmF htfpp #-}
module Main where
import Test.Framework
import Data.Char (isLower, isUpper, ord)
import Data.List (zipWith, sort)
letterFreqs :: String -> [Int]
letterFreqs str =
freqs' str (replicate 26 0)
where freqs' [] fs = fs
freqs' (ch:tail) fs
| isLower ch = let index = (ord ch - ord 'a')
(front, back) = splitAt index fs
newval = 1 + fs !! index
in freqs' tail (front ++ newval : drop 1 back)
| isUpper ch = let index = (ord ch - ord 'A')
(front, back) = splitAt index fs
newval = 1 + fs !! index
in freqs' tail (front ++ newval : drop 1 back)
| otherwise = freqs' tail fs
computeMaxBeauty :: String -> Int
computeMaxBeauty = sum . zipWith (*) [1..26] . sort . letterFreqs
test_ABbCcc = assertEqual 152 $ computeMaxBeauty "ABbCcc"
test_This_is_from_Facebook_Hacker_Cup_2013_ =
assertEqual 551 $ computeMaxBeauty "This is from Facebook Hacker Cup 2013."
test_Ignore_punctuation__please___ =
assertEqual 491 $ computeMaxBeauty "Ignore punctuation, please :)"
test_Sometimes_test_cases_are_hard_to_make_up_ =
assertEqual 729 $ computeMaxBeauty "Sometimes, test cases are hard to make up."
test_CodeReview_is_love__CodeReview_is_life_ =
assertEqual 724 $ computeMaxBeauty "CodeReview is love. CodeReview is life."
main :: IO ()
main = htfMain htf_thisModulesTests