Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat can be improved on my first haskell program?
    primarykey
    data
    text
    <p>Here is my first Haskell program. What parts would you write in a better way?</p> <pre><code>-- Multiplication table -- Returns n*n multiplication table in base b import Text.Printf import Data.List import Data.Char -- Returns n*n multiplication table in base b mulTable :: Int -&gt; Int -&gt; String mulTable n b = foldl (++) (verticalHeader n b w) (map (line n b w) [0..n]) where lo = 2* (logBase (fromIntegral b) (fromIntegral n)) w = 1+fromInteger (floor lo) verticalHeader :: Int -&gt; Int -&gt; Int -&gt; String verticalHeader n b w = (foldl (++) tableHeader columnHeaders) ++ "\n" ++ minusSignLine ++ "\n" where tableHeader = replicate (w+2) ' ' columnHeaders = map (horizontalHeader b w) [0..n] minusSignLine = concat ( replicate ((w+1)* (n+2)) "-" ) horizontalHeader :: Int -&gt; Int -&gt; Int -&gt; String horizontalHeader b w i = format i b w line :: Int -&gt; Int -&gt; Int -&gt; Int -&gt; String line n b w y = (foldl (++) ((format y b w) ++ "|" ) (map (element b w y) [0..n])) ++ "\n" element :: Int -&gt; Int -&gt; Int -&gt; Int -&gt; String element b w y x = format (y * x) b w toBase :: Int -&gt; Int -&gt; [Int] toBase b v = toBase' [] v where toBase' a 0 = a toBase' a v = toBase' (r:a) q where (q,r) = v `divMod` b toAlphaDigits :: [Int] -&gt; String toAlphaDigits = map convert where convert n | n &lt; 10 = chr (n + ord '0') | otherwise = chr (n + ord 'a' - 10) format :: Int -&gt; Int -&gt; Int -&gt; String format v b w = concat spaces ++ digits ++ " " where digits = if v == 0 then "0" else toAlphaDigits ( toBase b v ) l = length digits spaceCount = if (l &gt; w) then 0 else (w-l) spaces = replicate spaceCount " " </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload