Compute text area given letter heights and a text I have solved the designer pdf question with haskell. The code works. I am posting the code here to know how I could have done it better.


  
  First line contains the weight of each alphabet
  Second line contains the word.
  
  
  Sample input

1 3 1 3 1 4 1 3 2 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 7
zaba

  
  Output

28

  
  Explanation
  each character would take 1 space and multiply it with the max weight
  4 characters * 1 space * 7 weight = 28
  'z' has the max weight of 7.


Code

import Data.List
import Data.Maybe

getintval::(Maybe Int) -&gt; Int
getintval Nothing = 1
getintval (Just x) = x

solve'::[Char]-&gt;[(Char, Int)] -&gt; Int
solve' ch lst = k $  map getintval $ map (\x -&gt; finder' x) ch
  where
  k::[Int] -&gt; Int
  k fb = (*) (length ch)   $ foldl (\acc x -&gt; max acc x) 1 fb
  finder'::Char -&gt; Maybe Int
  finder' g =  case i of
            Just(x1,x2) -&gt; Just x2
            Nothing -&gt; Just 1
            where i = find(\(val1, val2) -&gt; val1 == g) lst


solve::[Char] -&gt; [Char] -&gt; Int
solve wght val = solve' val rec
  where
  rec::[(Char, Int)]
  rec = zipWith (\x y -&gt; (x, y)) ['a'..'z'] word1
  word1::[Int]
  word1 = map(read::String-&gt;Int) $ words wght

main::IO()
main = do
  weight &lt;- getLine
  pdfstr &lt;- getLine
  putStr . show $ solve weight pdfstr
getintval is just fromMaybe 1. finder always produces Just - you probably meant to map Nothing to Nothing. finder' is just lookup. foldl (\acc x -&gt; max acc x) 1 is just maximum (so long as ch is never empty). k can be inlined.

solve' :: [Char] -&gt; [(Char, Int)] -&gt; Int
solve' ch lst = (*length ch) $ maximum $ map (fromMaybe 1 . (`lookup` lst)) ch


zipWith (\x y -&gt; (x, y)) is just zip. read's type can be deduced. rec and word1 can be inlined.

solve :: [Char] -&gt; [Char] -&gt; Int
solve wght val = solve' val $ zip ['a'..'z'] $ map read $ words wght


mapMaybe throws away invalid characters. We can flatten the call tree by letting main assemble the pieces.

solve :: [Char] -&gt; [(Char, Int)] -&gt; Int
solve ch lst = (*length ch) $ maximum $ mapMaybe (`lookup` lst) ch

parse :: [Char] -&gt; [(Char, Int)]
parse = zip ['a'..'z'] . map read . words

main :: IO ()
main = do
  weight &lt;- getLine
  pdfstr &lt;- getLine
  print $ solve pdfstr $ parse weight