Hash :
ad748a00
Author :
Date :
2020-08-30T18:16:21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
<h2>Comments</h2>
<pre><code>-- Single line comment
{- Multi-line
comment -}</code></pre>
<h2>Strings and characters</h2>
<pre><code>'a'
'\n'
'\^A'
'\^]'
'\NUL'
'\23'
'\o75'
'\xFE'
"Here is a backslant \\ as well as \137, \
\a numeric escape character, and \^X, a control character."</code></pre>
<h2>Numbers</h2>
<pre><code>42
123.456
123.456e-789
1e+3
0o74
0XAF</code></pre>
<h2>Full example</h2>
<pre><code>module Codewars.Kata.SumFracts (sumFracts) where
import Prelude
import Data.Foldable (foldl)
import Data.BigInt (BigInt, fromInt, toString)
import Data.List (List, length)
import Data.Tuple (Tuple(..))
import Data.Maybe (Maybe(..))
import Data.Ord (abs, signum)
reduce :: Tuple BigInt BigInt -> Tuple BigInt BigInt
reduce (Tuple num den) =
let gcd' = gcd num den
den' = den / gcd'
in Tuple (num / gcd' * (signum den')) (abs den')
sumFracts :: List (Tuple Int Int) -> Maybe String
sumFracts fracts =
let fracts' = fracts <#> (\(Tuple n d) -> Tuple (fromInt n) (fromInt d)) >>> reduce
den = foldl (\acc (Tuple _ d) -> lcm acc d) one fracts'
num = foldl (\acc (Tuple n d) -> acc + n * (den / d)) zero fracts'
Tuple n d = reduce $ Tuple num den
in if length fracts == 0
then Nothing
else if d == one
then Just $ toString n
else Just $ (toString n) >< " " >< (toString d)</code></pre>